【问题标题】:Parsing json enconded object containing html entities in javascript在javascript中解析包含html实体的json编码对象
【发布时间】:2018-03-02 05:07:11
【问题描述】:

我有一个 php 对象..我使用 json_encode() 将其转换为 json 文本 功能。然后我将它传递给 javascript 上的 $.parseJSON() 函数。

注意:json 对象中的 description 字段包含以 html 实体编码的文本。

在执行 $.parseJSON() 函数时说...

SyntaxError: JSON.parse: bad control character in string literal at JSON数据的第1行第133列

但是,如果描述字段只是像 "description":"hy" 这样的普通文本,那么解析 $.parseJSON() 函数就没有问题。

需要我必须将描述字段与我在数据库中拥有的 html 实体一起存储..

帮帮我,伙计们...

$(window).load(function editPage(){
         var page_data = $.parseJSON('{"id":"1","title":"FAQ","summary":"Frequently Asked Questions are available here","description":"<!DOCTYPE html>\r\n<html>\r\n<head>\r\n<\/head>\r\n<body>\r\n<h6 class="section-heading__heading heading--1">What does Shopify do?<\/h6>\r\n<p> <\/p>\r\n<div class="grid-item grid-6 grid-push-1">\r\n<div class="long-form-content ">\r\n<p>Shopify is a complete <code><a class="body-link" href="https:\/\/www.shopify.com\/" target="_blank" rel="noopener">ecommrce solution<\/a><\/code> that allows you to set up an online store to sell your goods. It lets you organize your products, customize your storefront, accept credit card payments, track and respond to orders — all with a few clicks of the mouse.<\/p>\r\n<\/div>\r\n<\/div>\r\n<\/body>\r\n<\/html>","status":"1","featured_image":"Page-1519912947103.jpg","added_date":"2018-03-01 19:46:28","updated_date":"2018-03-02 10:13:11"}');
               console.log(page_data);
               }
            );

【问题讨论】:

  • hint .replace(/\r\n/g, '') - 修复它,但我不知道为什么! - 实际上我这样做了,JSON 值中不应该有 \r\n
  • 或者....replace(/\r\n/g, '\\r\\n') - 所以描述等现在将具有预期的\r\n
  • 非常感谢 Jaromanda 的帮助...根据您的说法,我将所有 \r 替换为 \\r 并将所有 \n 替换为 \\n ...并且它起作用了...跨度>

标签: javascript php json


【解决方案1】:

这是因为JSON.parse无法解析一些特殊字符,即\n、\t、\r和\f,需要在解析前替换掉。

$(window).load(function editPage(){
        var jsonString='{"id":"1","title":"FAQ","summary":"Frequently Asked Questions are available here","description":"<!DOCTYPE html>\r\n<html>\r\n<head>\r\n<\/head>\r\n<body>\r\n<h6 class="section-heading__heading heading--1">What does Shopify do?<\/h6>\r\n<p> <\/p>\r\n<div class="grid-item grid-6 grid-push-1">\r\n<div class="long-form-content ">\r\n<p>Shopify is a complete <code><a class="body-link" href="https:\/\/www.shopify.com\/" target="_blank" rel="noopener">ecommrce solution<\/a><\/code> that allows you to set up an online store to sell your goods. It lets you organize your products, customize your storefront, accept credit card payments, track and respond to orders — all with a few clicks of the mouse.<\/p>\r\n<\/div>\r\n<\/div>\r\n<\/body>\r\n<\/html>","status":"1","featured_image":"Page-1519912947103.jpg","added_date":"2018-03-01 19:46:28","updated_date":"2018-03-02 10:13:11"}';
        jsonString=jsonString.replace(/\n/g, "\\n")
            .replace(/\r/g, "\\r")
            .replace(/\t/g, "\\t")
            .replace(/\f/g, "\\f");
             var page_data = $.parseJSON(jsonString);
                   console.log(page_data);
                   }
                );

【讨论】:

  • 非常感谢您的帮助...我使用了您的代码..它很棒,但只有当我更改您的 .replace('\n', "\\n").replace 时它才有效('\r', "\\r").replace('\t', "\\t").replace('\f', "\\f") 函数到 .replace(/\r\n /g, '\\r\\n')
  • @Ahsit Tamang 当我从浏览器控制台尝试使用 '/\r\n/g ' 时,出现了一些错误。这就是我改变角色的原因。我不知道它在 php 中是如何工作的。如果我编辑它,你能把它标记为答案吗?
  • 请将代码中的 .replacee() 更改为 .replace() ..拼写错误
【解决方案2】:

经过有用的人的各种建议..我现在工作的代码是..

<script>
         $(window).load(function editPage(){

          var jsonString= '{"id":"1","title":"FAQ","summary":"Frequently Asked Questions are available here","description":"&lt;!DOCTYPE html&gt;\r\n&lt;html&gt;\r\n&lt;head&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n&lt;h6 class=&quot;section-heading__heading heading--1&quot;&gt;What does Shopify do?&lt;\/h6&gt;\r\n&lt;p&gt;&amp;nbsp;&lt;\/p&gt;\r\n&lt;div class=&quot;grid-item grid-6 grid-push-1&quot;&gt;\r\n&lt;div class=&quot;long-form-content &quot;&gt;\r\n&lt;p&gt;Shopify is a complete &lt;code&gt;&lt;a class=&quot;body-link&quot; href=&quot;https:\/\/www.shopify.com\/&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;ecommrce solution&lt;\/a&gt;&lt;\/code&gt; that allows you to set up an online store to sell your goods. It lets you organize your products, customize your storefront, accept credit card payments, track and respond to orders &amp;mdash; all with a few clicks of the mouse.&lt;\/p&gt;\r\n&lt;\/div&gt;\r\n&lt;\/div&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;","status":"1","featured_image":"Page-1519912947103.jpg","added_date":"2018-03-01 19:46:28","updated_date":"2018-03-02 10:13:11"}';
          jsonString=jsonString.replace(/\r\n/g, '\\r\\n');
          var page_data =$.parseJSON(jsonString);
          console.log(page_data);
           }
        );
       </script>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-12-07
    • 2018-12-27
    • 2011-10-27
    • 2018-01-14
    • 2013-09-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多