簡體   English   中英

解析javascript中包含html實體的json加密對象

[英]Parsing json enconded object containing html entities in javascript

我有一個php對象。使用json_encode()函數將其轉換為json文本。 然后,我在javascript的$ .parseJSON()函數上傳遞了它。

注意:json對象中的description字段包含以html實體編碼的文本。

在執行$ .parseJSON()函數時說...

SyntaxError:JSON.parse:JSON數據第1行第133列的字符串文字中的控制字符錯誤

但是,如果description字段僅具有普通文本,例如“ 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);
               }
            );

這是因為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);
                   }
                );

經過各方面的建議,我的工作代碼是..

<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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM