我必须将 JSON-LD 数据的多个 script
元素注入(inject)到我的应用程序的 head
中,所有元素都属于相同的 @type
。这是因为从不同的数据源提取不同的字段。
这种重复会导致任何问题吗?
<script type="application/ld+json">
{
"@type": "Organisation",
"name": "John Smith"
}
</script>
<script type="application/ld+json">
{
"@type": "Organisation",
"city": "London"
}
</script>
我希望 Google 能够将其简单地翻译为:
<script type="application/ld+json">
{
"@type": "Organisation",
"name": "John Smith",
"city": "London"
}
</script>
是吗?
最佳答案
消费者不能/不应该假设这些 JSON 对象描述相同的事物。 (想象一个包含许多不同组织信息的网页:假设它们是同一组织当然是错误的。)
JSON-LD 允许您指定不同对象中描述的内容是相同的:为它们提供相同的 @id
值。
@id
采用 IRI 作为标识符(它是 useful to provide them for many reasons )。
参见Node Identifiers在 JSON-LD 规范中。
所以它可能看起来像这样(使用 Schema.org 而不是您的自定义词汇表):
<script type="application/ld+json">
{
"@context": "http://schema.org",
"@type": "Organization",
"@id": "/organizations/42#this",
"name": "ACME"
}
</script>
<script type="application/ld+json">
{
"@context": "http://schema.org",
"@type": "Organization",
"@id": "/organizations/42#this",
"description": "…"
}
</script>
(相对 URL /organizations/42#this
将代表组织本身。最好的做法是在 /organizations/下提供此 JSON-LD 以及有关组织的信息42
.)
关于html - head 中存在重复的 JSON-LD 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45876849/