【问题标题】:Replace &quot with double quotes json将 &quot 替换为双引号 json
【发布时间】:2022-11-23 14:53:28
【问题描述】:

我在 webflow(一个网站平台)工作,并在内容字段中粘贴一个 JSON-LD 脚本。该字段映射到网页的部分。

JSON 脚本中的所有双引号 " 都被切换为 "

是否有一行代码可以添加到脚本中以将它们切换回来?

我尝试在 </script> 标签之前插入以下内容 .replace(/"/g, '\"') 和.replace(/"/g, '\\"') 没有一个工作。

这是代码在发布之前在后端的样子

<script type='application/ld+json'>
{Schema}
.replace(/&quot;/g, '\\"')
</script>

这就是它在实时站点上呈现的内容

<script type='application/ld+json'>

{ &quot;@context&quot;: &quot;https://schema.org&quot;, &quot;@type&quot;: &quot;FAQPage&quot;, &quot;mainEntity&quot;: [{ &quot;@type&quot;: &quot;Question&quot;, &quot;name&quot;: &quot;How quickly can I get my certificate of insurance?&quot;, &quot;acceptedAnswer&quot;: { &quot;@type&quot;: &quot;Answer&quot;, &quot;text&quot;: Certificates are usually issued 24 hours after the policy is bound.&quot; } }]
}
.replace(/&quot;/g, '\\"')
</script>

【问题讨论】:

    标签: javascript html json content-management-system


    【解决方案1】:

    .replace 需要附加到一个字符串变量,所以不幸的是你不能在你的架构的末尾使用它。脚本标签也是 application/ld+json(不是 /javascript),因此 javascript 将无法在标签内运行。

    这个问题的核心是 webflow 将你输入的所有内容都放入块中,将其视为网络安全文本,然后发布它,因此脚本标签内的修复将在此之前运行(然后被覆盖).

    所以它看起来像是您正在使用的内容块的问题。您是否能够从标准文本块移动到 Rich Text Format 块,或专门用于代码的块?

    It looks like some people are having a similar issue in this webflow support thread

    祝你好运!

    【讨论】:

      【解决方案2】:
      1. 首先您错过了 json 中 Certificates 单词之前的 " 目的
      2. 您不能将 js 函数运行到类型的脚本中应用程序/ld+json
      3. 检查这个从 JSON 中删除 " 的示例

        <script type='application/ld+json'>
        
        { 
          &quot;@context&quot;: &quot;https://schema.org&quot;, 
          &quot;@type&quot;: &quot;FAQPage&quot;, 
          &quot;mainEntity&quot;: 
          [
            { 
              &quot;@type&quot;: &quot;Question&quot;, 
              &quot;name&quot;: &quot;How quickly can I get my certificate of insurance?&quot;,
              &quot;acceptedAnswer&quot;:
              {
                &quot;@type&quot;: &quot;Answer&quot;, 
                &quot;text&quot;: &quot;Certificates are usually issued 24 hours after the policy is bound.&quot;
              } 
            }
          ]
        }
        </script>
        <script>
        var jsonld = document.querySelector('script[type="application/ld+json"]').innerText;
        jsonld = jsonld.replaceAll("&quot;", '"');
        jsonld = JSON.parse(jsonld);
        console.log(jsonld);
        </script>

      【讨论】:

        猜你喜欢
        • 2023-04-07
        • 2018-09-05
        • 2018-11-18
        • 1970-01-01
        • 1970-01-01
        • 2017-12-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多