如何在 JSON-LD 中使用多个元素

ber*_*rdh 2 schema.org json-ld

我有一个描述旅游景点的页面 ( TouristAttraction)。由于我还想添加面包屑信息,因此我也需要添加WebPage

添加两个信息的方法是什么:

  1. 我应该使用WebPage并添加TouristAttractionasmainEntity吗?
  2. 我应该script使用单独的 WebPageTouristAttraction块创建 2 个单独的 JSON-LD块吗?

当使用 2 个实体时:

  1. 我必须在两个实体中提供主要信息(名称、图像、评级等),还是只提供一个(哪一个)?

ber*_*rdh 8

@unor 的答案几乎是正确的,但是如果您是为 google serp 做的,则只有将其拆分为单独的 json 块(或图形符号)才能获得最佳结果。

假设您想使用Recipe实体在 serps 中获取 Recipies 的 google 丰富片段,您可以这样做:

<script type="application/ld+json">
{
  "@context":"https:\/\/schema.org",
  "@type":"Recipe",
  "name":"Example",
  "image":"https:\/\/www.example.com"
}
</script>
Run Code Online (Sandbox Code Playgroud)

在谷歌的结构化数据测试工具中,您将获得一个预览按钮:

在此处输入图片说明

如果您现在想要添加来自其他实体的其他信息(如面包屑),您必须使用单独的 JSON-LD 块,否则您将无法获得预览按钮。所以例如

<script type="application/ld+json">
{
    "@context": "http://schema.org",
    "@type": "ItemPage",
    "breadcrumb": {
        "@type": "BreadcrumbList"
    },
    "mainEntity": {
        "@type":"Recipe",
        "name":"Example",
        "image":"https:\/\/www.example.com"
    }
}
</script>
Run Code Online (Sandbox Code Playgroud)

有效,但不会显示预览按钮。

但是如果你把它分开,它会显示为单独的实体和预览按钮:

<script type="application/ld+json">
    {
        "@context": "http://schema.org",
        "@type": "ItemPage",
        "breadcrumb": {
            "@type": "BreadcrumbList"
        }
    }
</script>
<script type="application/ld+json">
{
    "@context": "http://schema.org",
        "@type":"Recipe",
        "name":"Example",
        "image":"https:\/\/www.example.com"
    }
}
</script>
Run Code Online (Sandbox Code Playgroud)

同样适用于数组符号:

<script type="application/ld+json">
[
    {
        "@context": "http://schema.org",
        "@type": "ItemPage",
        "breadcrumb": {
            "@type": "BreadcrumbList"
        }
    },
    {
        "@context": "http://schema.org",
         "@type":"Recipe",
         "name":"Example",
         "image":"https:\/\/www.example.com"
    }
]
</script>
Run Code Online (Sandbox Code Playgroud)

和图表:

<script type="application/ld+json">
{
  "@context": "http://schema.org",
  "@graph": 
    [
        {
            "@type": "ItemPage",
            "breadcrumb": {
                "@type": "BreadcrumbList"
            }
        },
        {
            "@type":"Recipe",
            "name":"Example",
            "image":"https:\/\/www.example.com"
        }
    ]
}
</script>
Run Code Online (Sandbox Code Playgroud)

归功于@unor(另请参阅如何组合多个 JSON-LD 标记?