【问题标题】:Representing repeating attributes in JSON-LD表示 JSON-LD 中的重复属性
【发布时间】:2018-07-05 17:42:37
【问题描述】:

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

允许有一个数组作为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>

【问题讨论】:

    标签: schema.org json-ld


    【解决方案1】:

    最简单(也是最受支持)的方法是提供一个数组作为值,就像在你的 sn-p 中一样:

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

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

    With @id,您可以给每个项目一个 URI,并将此 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,您还可以在每个Commentwith @reverse 中引用Article

    {
      "@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"}}
        }
      ]
    }
    

    【讨论】:

    • (非常好,感谢 unor!)为了 100% 完成,我更新了您的代码快照以获取(Google 工具)等效于 myExample 的有效示例。参看。要点gist.github.com/boly38/0935d8b0a83b7a2fe4254fa2a6b92113
    • 感谢@unor 分享这个。我玩了几个小时,将一系列评论与产品联系起来。看来(至少谷歌结构化数据测试工具)还不能理解named graphs
    猜你喜欢
    • 2014-10-10
    • 2017-04-11
    • 1970-01-01
    • 2021-08-27
    • 1970-01-01
    • 1970-01-01
    • 2021-09-25
    • 2018-02-03
    • 1970-01-01
    相关资源
    最近更新 更多