Schema.org - JSON-LD - 放在哪里?

Schema.org - JSON-LD - Where to Place?

我希望将 JSON-LD 用于网站架构。 (模式表示 schema.org 数据。)我知道如何写入数据,但我的问题是我的代码中是否有一个首选位置来插入此数据?换句话说,JSON-LD 是否应该始终在 headbody 等中?

从Schema.org、JSON-LD,以及可能抽取的RDF来看,应该无所谓。无论数据是从文档中的哪个位置提取的,数据都是相同的。

从HTML5的角度来看:

如果它是关于您的页面的数据(或此页面的内容),您可以将 script 元素放在 head 中,作为 head element

[…] represents a collection of metadata for the Document

当然,使用 body 也不会错。只是您不应该将 head 用于与您的页面或其所代表的内容无关的数据。

数据可以放在任何地方。来自 Google's documentation:

The data, enclosed within the <script type="application/ld+json"> ... </script> tags as shown in the examples below, may be placed in either the <HEAD> or <BODY> region of the page that displays that event.

你也可以使用data dynamically fetched using AJAX:

JSON-LD markup inserted by Javascript that runs upon initial page load can be recognized.

更新(如评论中Antony所指出)

latest documentation 说:

[JSON-LD is a] JavaScript notation embedded in a tag in the page head or body... Google can read JSON-LD data when it is dynamically injected into the page's contents, such as by JavaScript code or embedded widgets in your content management system.

如果你选择插入<body>,你必须这样做:

<p class="companyName" vocab="http://schema.org/" resource="#manu" typeof="Organization">
   <span property="name">ShopTech Media</span>
   <img property="logo" src="https://yoursite.com/logo.png" />
   <a property="url" href="http://www.yoursite.com">Home page</a>
</p>
<p typeof="contactPoint">
  <span property="contactType">Customer Service:</span>
<span property="telephone">+45-xxxxxxx</span>
</p>

下面是将结构化数据插入 <head> 标签的脚本代码

<script type="application/ld+json"> 
{
  "@context": "https://schema.org",
  "@type": "Organization",
  "url": "http://www.shoptech.media",
  "logo": "https://shoptech.media/wp-content/uploads/2019/08/cropped-logo-sm.png",
  "contactPoint": [{
    "@type": "ContactPoint",
    "telephone": "+45-65711114",
    "contactType": "customer service"
  }]
}
</script>

general structured data guideline

查看文档