为 Thymeleaf 的内联指定双引号 Javascript

Specify Double Quotes for Thymeleaf's inline Javascript

我正在尝试向使用 Thymeleaf 构建的站点添加一些架构标记。我的第一个想法是使用 ld+json 方法:

<script type="application/ld+json" th:inline="javascript">
{
    "@context": "http://schema.org",
    "@type": "LocalBusiness",
    "address": {
      "@type": "PostalAddress",
      "streetAddress": /*[[ ${C:Location.street}]]*/,
      "addressLocality": /*[[ ${C:Location.city}]]*/,
      "addressRegion": /*[[ ${C:Location.state}]]*/,
      "postalCode": /*[[ ${C:Location.zipcode}]]*/
    },
}
</script>

但是 Thymeleaf 以单引号输出这些字符串,在使用 https://developers.google.com/structured-data/testing-tool/

检查时显然没有验证为正确 JSON

是否可以告诉 Thymeleaf 在这里使用双引号?如果一切都失败了,我可以做 HTML 微数据标记,但我不想这样做,因为它不那么漂亮和模块化。

我尝试使用文本模式:

<script type="application/ld+json" th:inline="text">
    {
        "@context": "http://schema.org",
        "@type": "EmailMessage",
        "potentialAction": {
            "@type": "ViewAction",
            "url": "[[ @{${url}} ]]",
            "name": "[[ #{message.button.text} ]]"
        }
    }
</script>

输出:

<script type=3D"application/ld+json" xml:space=3D"preserve">
    {
        "@context": "http://schema.org",
        "@type": "EmailMessage",
        "potentialAction": {
            "@type": "ViewAction",
            "url": "https://watch-movies.com/watch",
            "name": "Watch movie"
        }
    }
</script>

这可能对使用 html.erb

的人有帮助
<script type="application/ld+json">
  {
      "@context": "http://schema.org",
      "@type": "EmailMessage",
      "potentialAction": {
        "@type": "ViewAction",
        "url": <%= resource.photos.first(3).map(&:url) %>,
        "name": "some text"
      }
  }
</script>

在这里,您将得到 [&quote;url1&quote;, &quote;url2&quote; &quote;url3&quote;].

,而不是 url 字段的 ["url1", "url2", "url3"]

要获得所需的结果,请使用 .to_json.html_safe

<script type="application/ld+json">
  {
    "@context": "http://schema.org",
    "@type": "EmailMessage",
    "potentialAction": {
        "@type": "ViewAction",
        "url": <%= resource.photos.first(3).map(&:url).to_json.html_safe %>,
        "name": "some text"
    }
  }
</script>