表示JSON-LD中的重复属性

时间:2018-07-04 18:59:24

标签: schema.org json-ld

我想知道如何使用JSON-LD(由Google提供的recommended和Schema.org规范来表示重复属性。例如,我们应该如何用 N Article s(N> 1)个Comment来表示?

像下面的示例一样,允许将数组作为comment值。 Google testing tool喜欢它似乎是。

但是可以使用 flat @graph表示法代替吗?怎么样?我必须开发一个站点,并且这种表示形式可能更易于实现。

我想两者都允许吗?那怎么选择呢?

我的例子:

<script type="application/ld+json">
{
  "@context" : "http:\/\/schema.org",
  "@type" : "Article",
  "url" : "https:\/\/exemple.com/article?id=1234",
  "author" :{"@type" : "Person","name" : "Didier"},
  "image" : "https:\/\/exemple.com/article.jpg",
  "mainEntityOfPage" : "https:\/\/exemple.com",
  "dateModified" : "2018-06-14T19:50:02+02:00",
  "datePublished" : "2018-06-14T19:50:02+02:00",
  "publisher" : {"@type" : "Organization","name" : "exemple.com", "logo" : {"@type" : "ImageObject", "url" : "https:\/\/exemple.com\/logo.png"}},
  "headline" : "my article",
  "text" : "blah blah",
  "commentCount" : 2,
  "comment" : [{
      "author" : {"@type" : "Person", "name" : "Didier"},
      "text" : "comment first!!",
      "dateCreated" : "2018-06-14T21:40:00+02:00"
},{
      "author" : {"@type" : "Person", "name" : "Robert"},
      "text" : "second comment",
      "dateCreated" : "2018-06-14T23:23:00+02:00"
}]
}
</script>

1 个答案:

答案 0 :(得分:4)

最简单(也是最受支持)的方法是提供一个数组作为值,就像您的代码段一样:

"comment": [
  {"@type": "Comment"},
  {"@type": "Comment"}
]

如果要使用多个顶级项目(with @graph or other options),则需要一种方式来传达这些顶级Comment项目是Article的注释。

With @id,您可以为每个项目指定一个URI并将其作为属性值引用,而不是嵌套该项目:

{
  "@context": "http://schema.org",
  "@graph": [
    {
      "@type": "Article",
      "@id": "/articles/foobar",
      "comment": [
        {"@id": "/articles/foobar#comment-1"},
        {"@id": "/articles/foobar#comment-2"}
      ]
    },
    {
      "@type": "Comment",
      "@id": "/articles/foobar#comment-1"
    },
    {
      "@type": "Comment",
      "@id": "/articles/foobar#comment-2"
    }
  ]
}

除了在Article下列出注释URI,还可以在每个Articlewith @reverse中引用Comment

{
  "@context": "http://schema.org",
  "@graph": [
    {
      "@type": "Article",
      "@id": "/articles/foobar"
    },
    {
      "@type": "Comment",
      "@id": "/articles/foobar#comment-1",
      "@reverse": {"comment": {"@id": "/articles/foobar"}}
    },
    {
      "@type": "Comment",
      "@id": "/articles/foobar#comment-2",
      "@reverse": {"comment": {"@id": "/articles/foobar"}}
    }
  ]
}