这是JSON-LD索引图的正确使用吗?

时间:2015-03-03 16:10:25

标签: json-ld

我想用json-ld格式创建一个人的集合,但是我需要保留一些键而不是使用数组,所以我先尝试了这个:

{
  "@context" : {
    "@base" : "http://www.example.com/data/",
    "@vocab" : "http://www.example.com/vocab#",
    "name" : "schema:name",
    "people" : {
      "@container" : "@index",
      "@id" : "people"
    },
    "schema" : "http://schema.org/"
  },
  "@id" : "http://www.example.com",
  "people" : {
    "person1" : {
      "@id" : "people/person1",
      "name" : "Person 1"
    },
    "person2" : {
      "@id" : "people/person2",
      "name" : "Person 2"
    },
    "person3" : {
      "@id" : "people/person3",
      "name" : "Person 3"
    },
    "person4" : {
      "@id" : "people/person4",
      "name" : "Person 4"
    }
  }
}

在JSON-LD Playground中测试一切看起来很好,N-Quads结果显示如预期的东西:

<http://www.example.com/data/people/person1> <http://schema.org/name> "Person 1" .
<http://www.example.com/data/people/person2> <http://schema.org/name> "Person 2" .
<http://www.example.com/data/people/person3> <http://schema.org/name> "Person 3" .
<http://www.example.com/data/people/person4> <http://schema.org/name> "Person 4" .
<http://www.example.com> <http://www.example.com/vocab#people> <http://www.example.com/data/people/person1> .
<http://www.example.com> <http://www.example.com/vocab#people> <http://www.example.com/data/people/person2> .
<http://www.example.com> <http://www.example.com/vocab#people> <http://www.example.com/data/people/person3> .
<http://www.example.com> <http://www.example.com/vocab#people> <http://www.example.com/data/people/person4> .

然后我想添加一个模式:knows属性也保留密钥而不使用这样的数组:

{
  "@context" : {
    "@base" : "http://www.example.com/data/",
    "@vocab" : "http://www.example.com/vocab#",
    "knows" : {
      "@container" : "@index",
      "@id" : "schema:knows",
      "@type" : "@id"
    },
    "name" : "schema:name",
    "people" : {
      "@container" : "@index",
      "@id" : "people"
    },
    "schema" : "http://schema.org/"
  },
  "@id" : "http://www.example.com",
  "people" : {
    "person1" : {
      "@id" : "people/person1",
      "knows" : {
        "person2" : "people/person2",
        "person3" : "people/person3"
      },
      "name" : "Person 1"
    },
    "person2" : {
      "@id" : "people/person2",
      "name" : "Person 2"
    },
    "person3" : {
      "@id" : "people/person3",
      "name" : "Person 3"
    },
    "person4" : {
      "@id" : "people/person4",
      "name" : "Person 4"
    }
  }
}

这次我在操场上测试时会返回以下错误:

  

{&#34; name&#34;:&#34; jsonld.SyntaxError&#34;,&#34; message&#34;:&#34;无效的JSON-LD语法;   检测到冲突的@index属性。&#34;,&#34;详细信息&#34;:{&#34; code&#34;:   &#34;冲突的索引&#34;,&#34;主题&#34;:{&#34; @ id&#34;:   &#34; http://www.example.com/data/people/person2&#34;,&#34; @ index&#34;:&#34; person2&#34; }}   }

那么保留密钥并从不使用数组或使用索引映射的正确方法的替代方法是什么?

此致

1 个答案:

答案 0 :(得分:2)

这是对的。您在jsonld.js(which I already reported)中发现了一个错误。您可以尝试运行示例in my playground或使用Gregg Kellogg's RDF distiller service。两者都返回你期望的三元组。