@unor 的回答几乎是正确的,但如果你是为 google serp 做的,那么只有将它分成单独的 json 块(或图形符号)才能得到最好的结果。
假设您想使用 Recipe 实体在 serps 中获取 google's rich snippet for Recipies,您可以这样做:
<script type="application/ld+json">
{
"@context":"https:\/\/schema.org",
"@type":"Recipe",
"name":"Example",
"image":"https:\/\/www.example.com"
}
</script>
在 google 的 Structed Data Testing Tool 中,您将获得一个预览按钮:
如果您现在想要添加来自其他实体的其他信息(如面包屑),则必须使用单独的 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>
有效,但不会显示预览按钮。
但如果你把它分开,它会显示到单独的实体和预览按钮:
<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>
同样适用于 Array-Notation:
<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>
还有图表:
<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>
学分归@unor(另见How do you combine several JSON-LD markups?)