在javascript中解析包含html实体的json编码对象

问题描述 投票:1回答:2

我有一个php对象..我使用json_encode()函数转换为json文本。然后我在javascript上传递$ .parseJSON()函数。 注意:json对象中的description字段包含以html实体编码的文本。 执行时$ .parseJSON()函数说...

SyntaxError:JSON.parse:JSON数据的第1行第133行的字符串文字中的错误控制字符

但是,如果描述字段只是像普通文本那样“描述”:“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);
               }
            );
javascript php json
2个回答
2
投票

这是因为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);
                   }
                );

0
投票

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

<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>
© www.soinside.com 2019 - 2024. All rights reserved.