繁体   English   中英

用双引号替换 " json

[英]Replace &quot with double quotes json

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

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

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

我尝试在</script>标记.replace(/&quot;/g, '\"').replace(/&quot;/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>

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

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

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

看起来有些人在此 webflow 支持线程中遇到了类似的问题

祝你好运!

  1. 首先你错过了 json object 中 Certificates 单词之前的 "
  2. 您不能将 js function 运行到application/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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM