首页 > 解决方案 > 从 JSON-LD 中的前缀和数据构造 IRI

问题描述

在将未编辑的 JSON 数据转换为 JSON-LD 时,使用前缀和数据值为对象构造 IRI 时遇到问题。我运行的示例代码是:

{ 
    "@context" : 
    { "prefix" : "http://www.gerastree.at/",
      "rdfs" : "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
      "@vocab" : "http://example.com/" ,
      "load" : "prefix:load"
       "items" : "prefix:item"

    }, 
    "@type" : "tree",
    "@id" : "prefix:t1" , 
    "items" : 
    [
        { "@id" : "prefix:t2",
          "@type" : "item",
          "load" : "some111"
         },
        { "@id" : "prefix:t3",
          "@type" : "item",
          "load" : "some2222"
         }    
    ]
}

但是,当我将@id值从“prefix:t1”更改为原始 JSON 中的纯数据值(即仅更改为“t1”、“t2”和“t3”)时,不再处理对象。不正确的 JSON-LD 代码(至少不被 阅读riot

{ 
    "@context" : 
    { "prefix" : "http://www.gerastree.at/",
      "rdfs" : "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
       "@vocab" : "http://example.com/" ,
       "load" : "prefix:load"
       "items" : "prefix:item"

    }, 
    "@type" : "tree",
    "@id" : "t1" , 
    "items" : 
    [
        { "@id" : "t2",
          "@type" : "item",
          "load" : "some111"
         },
        { "@id" : "t3",
          "@type" : "item",
          "load" : "some2222"
         }    
    ]
}

值“t1”等是唯一的,我想将它们与前缀一起用作 IRI,以将数据与其他数据链接起来。有没有一种方法可以在不更改生成 JSON 数据的程序或编辑文件的情况下生成 IRI,并在上下文中添加一些内容。

我找到了一个解决方案(基于Json-LD > Define a "person" for easy use as values on different keys for WebPage schema 的解决方案),但不明白它为什么起作用。

{ 
    "@context" : 
    { "prefix" : "http://www.gerastree.at/",
      "rdfs" : "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
       "@base" : "http://example.com/" ,
      "load" : "prefix:load",
     "items" : "prefix:item"

    }, 
    "@type" : "tree",
    "@id" : "t1" , 
    "items" : 
    [
        { "@id" : "t2",
          "@type" : "item",
          "load" : "some111"
         },
        { "@id" : "t3",
          "@type" : "item",
          "load" : "some2222"
         }    
    ]
}

我不认为这是为@type 的@id 或属性的值指定默认URI 的任何方式的重复?.

需要对上下文进行哪些添加和更改?

标签: json-ld

解决方案


通过对 JSON-LD 建议和实验的更多阅读,我找到了为什么我的第三个版本可以正常工作的解释。

@vocab应用于属性和对象仅 @base用于完成主体的 IRI。

不是很明显,但对于我的应用程序来说足够灵活。


推荐阅读