是否应该转义 JSON-LD 字符串?

neb*_*ous 9 escaping schema.org json-ld

JSON-LD 内容中的字符串值是否应该转义?例如,Google 建议使用以下内容为站点搜索提供提示:

<script type="application/ld+json">
{
   "@context": "http://schema.org",
   "@type": "WebSite",
   "url": "https://www.example-petstore.com/",
   "potentialAction": {
     "@type": "SearchAction",
     "target": "https://query.example-petstore.com/search?q={search_term_string}",
     "query-input": "required name=search_term_string"
   }
}
</script>
Run Code Online (Sandbox Code Playgroud)

但是,如果我网站的搜索 URL 包含多个查询参数怎么办?是否应该或可以转义目标值中的字符?例如:

"target": "https://query.example-petstore.com/search?foo=bar\u0026q={search_term_string}",
Run Code Online (Sandbox Code Playgroud)

在 JSON-LD 中进行标记时,同样的问题适用于几种常见的 schema.org 类型。Organization->sameAs 中的 Google+ 社交个人资料链接,例如:如果我的组织的个人资料是

https://plus.google.com/+BeardsAreSweet
Run Code Online (Sandbox Code Playgroud)

应该表示为:

"sameAs": ["https://plus.google.com/+BeardsAreSweet"]
Run Code Online (Sandbox Code Playgroud)

或者

"sameAs": ["https://plus.google.com/\u002bBeardsAreSweet"]
Run Code Online (Sandbox Code Playgroud)

更重要的是,这真的重要吗?

bhs*_*cer 1

当 url 是 JSON 对象的一部分时,无需对其进行转义。有效的 url 字符串是有效的 JSON 字符串。JSON 字符串可以包含:

除 \ 或 " 或控制字符之外的任何 UNICODE 字符

http://json.org

  • 问题还没有得到解答!我还想知道:当 LD-JSON 中的参数应具有值“https://example.com/search?q=abc&amp;filter=true”时,当我使用转义版本“https://example”时可以吗.com/search?q=abc&amp;filter=true` ??? 请帮忙 (3认同)
  • 我主要关心的不是 json 中允许的内容,而是如何解释字符串。例如,在“sameAs”示例中,谷歌会平等地解释这两个字符串吗?它们在解码时都表示相同的值,但是它们是在评估之前解码的吗?我在规范中没有看到任何相关内容。 (2认同)