【问题标题】:What is the proper way to reference another resource in JSON-LD在 JSON-LD 中引用另一个资源的正确方法是什么
【发布时间】:2016-09-23 17:18:40
【问题描述】:

所以我试图围绕 JSON-LD 进行思考,我看到的所有示例主要包括嵌入“链接数据”。但我想提供对链接数据的引用(主要是因为将其全部嵌入可能会导致 10MB 有效负载)。所以我想知道我这样做是否正确。

这是我所拥有的:

{
  "@context": "/contexts/Customers.jsonld",
  "@id": "/customers/1",
  "@type": "Customer",
  "sessions": {
    "@id": "/customers/1/sessions",
    "@type": "Session"
  },
  "dealer": "/dealers/2",
  "givenName": "Abe",
  "familyName": "Abrahms",
  /* ... snip ... */
}

我在这里谈论的链接数据的引用由sessions 属性表示。假设这是正确的,那么我需要在 Customer 上下文中进行哪些更改?

"@context": {
    "hydra": "http://www.w3.org/ns/hydra/core#",
    "doc": "https://api.waterlinkconnect.com/doc#",
    "Customer": "doc:Customer",
    "givenName": "doc:Customer/givenName",
    "familyName": "doc:Customer/familyName",
    "email":"doc:Customer/email",
    "address":"doc:Customer/address",
    "notes":"doc:Customer/notes",
    "phone1":"doc:Customer/phone1",
    "phone2":"doc:Customer/phone2"
    "sessions": "???????"
}

【问题讨论】:

    标签: json-ld


    【解决方案1】:

    您只需提供 IRI 作为值,例如:

    "propertyFoo": { "@id": "https://example.com/some-iri" }
    

    (此处使用@id,这样IRI 就不会被解释为字符串值。)

    因此,您使用 sessions 的示例很好,但如果您不想要/不需要,您没有提供 @type

    type coercion

    如果此属性始终获取 IRI 作为值,您可以在 @context 中定义它:

    "propertyFoo": 
    { 
      "@id": "https://your-vocabulary.example.com/propertyFoo",
      "@type": "@id"
    }
    

    那么你可以在提供值时省略@id

    "propertyFoo": "https://example.com/some-iri" 
    

    (如果像这样使用类型强制,则不能为该节点提供额外的属性。)

    【讨论】:

    • "property": { "https://example.com/some-iri" } 那不是无效的 JSON,还是我误解了?
    • @CodingGorilla:啊,对(我复制粘贴了前面的示例,但没有删除足够的内容^^),谢谢!我编辑了我的答案并删除了{}
    • 谢谢,最后一个问题,当我在 json-ld 操场上使用 "property": { "@type": "@id" } 测试修改后的上下文时,我收到错误:Invalid JSON-LD syntax; @context terms must define an @id. 显然它希望我添加一个 @987654336 @ 对于属性,我假设 @id 将是 Session 的上下文,对吗?
    • @CodingGorilla:它必须是该属性的 IRI(因为将术语映射到 IRI 是 @context 的工作)。因此,如果您使用的是 Schema.org 定义的 image,那么它将是 "@id": "http://schema.org/image"。如果您使用未由其他词汇定义的属性(即您自己的/自定义属性),您最好在自己的域下使用 IRI(使其全局唯一)。
    • 完美!再次感谢您提供更多信息!
    猜你喜欢
    • 2019-04-14
    • 1970-01-01
    • 2018-08-16
    • 1970-01-01
    • 1970-01-01
    • 2021-08-23
    • 1970-01-01
    • 2014-05-31
    • 2011-09-09
    相关资源
    最近更新 更多