【问题标题】:How can i output a json-object inside a html-file?如何在 html 文件中输出 json 对象?
【发布时间】:2025-12-23 14:40:09
【问题描述】:

我正在使用中间人生成 HTML,我希望输出一个 json-ld-object,如下所示:

<script type="application/ld+json">
{
  "@context": "http://schema.org",
  "@type": "Person",
  "name": "John Doe",
  "jobTitle": "Graduate research assistant",
  "affiliation": "University of Dreams",
  "additionalName": "Johnny",
  "url": "http://www.example.com",
  "address": {
    "@type": "PostalAddress",
    "streetAddress": "1234 Peach Drive",
    "addressLocality": "Wonderland",
    "addressRegion": "Georgia"
  }
}
</script>

我在一个对象中以正确的方式构造了数据,有没有好的方法来输出它?

【问题讨论】:

标签: ruby-on-rails ruby erb


【解决方案1】:

要以正确的格式输出它应该是这样的:

%script{type: 'application/ld+json'}
:plain
  {
    "@context": "http://schema.org",
    "@type": "Person",
    "name": "John Doe",
    "jobTitle": "Graduate research assistant",
    "affiliation": "University of Dreams",
    "additionalName": "Johnny",
    "url": "http://www.example.com",
    "address": {
      "@type": "PostalAddress",
      "streetAddress": "1234 Peach Drive",
      "addressLocality": "Wonderland",
      "addressRegion": "Georgia"
    }
  }

【讨论】: