简体   繁体   English

JSON.parse()无法使用HTML解析字符串

[英]JSON.parse() cannot parse string with HTML

I checked many questions but didn't see a solution. 我检查了许多问题,但没有找到解决方案。

I have JSON which was STRINGIFIED (containing HTML). 我有STRINGIFIED(包含HTML)的JSON。 This JSON is stored as a string on an element. 此JSON作为字符串存储在元素上。 When I try to JSON.parse(str) I am getting errors 当我尝试JSON.parse(str)时出现错误

I know that the errors are due to the special symbols \\n \\t etc and has to be escaped. 我知道错误是由于特殊符号\\ n \\ t等引起的,必须转义。 So how to parse this string which is the JSON and have it as an object. 因此,如何解析此字符串(即JSON)并将其作为对象。

 var obj = { "blocks": [{ "frames_content": "<div id=\\"page\\" class=\\"page\\">\\n \\n \\t<div class=\\"container\\" id=\\"divider1\\">\\n \\n \\t\\t<div class=\\"col-md-12\\">\\n \\n \\t\\t\\t<hr class=\\"dashed\\" data-selector=\\"hr.dashed\\" style=\\"outline: none; cursor: inherit;\\">\\n \\t\\n \\t\\t</div><!-- /.col -->\\n \\t\\n \\t</div><!-- /.container -->\\n \\n </div>", "frames_sandbox": false, "frames_loaderFunction": "", "frames_height": 162, "frames_original_url": "/static/admin-users/pages/elements/divider1.html" }] }; document.getElementById("debug").innerHTML = JSON.stringify(obj); var str = document.getElementById("debug").innerHTML; var x = JSON.parse(str); console.log(x); 
 <div id="debug"></div> 

===== UPDATE =====更新

If I read the element with .text() works 如果我使用.text()读取元素,

I have one more scenario which I don't get how to solve 我还有另外一种情况,我无法解决

<script>
var str = '{"pages":{"index":{"blocks":[{"frames_content":"<div id=\"page\" class=\"page\">\n    \n    \t<div class=\"container\" id=\"divider1\">\n    \n    \t\t<div class=\"col-md-12\">\n    \n    \t\t\t<hr class=\"dashed\" data-selector=\"hr.dashed\" style=\"outline: none; cursor: inherit;\">\n    \t\n    \t\t</div><!-- /.col -->\n    \t\n    \t</div><!-- /.container -->\n    \n    </div>","frames_sandbox":false,"frames_loaderFunction":"","frames_height":162,"frames_original_url":"/static/admin-users/pages/elements/divider1.html"}]}},"responsiveMode":"desktop"}';

var obj = JSON.parse(str);
</script>

Should I escape them with REGEX or what ? 我应该用REGEX还是它们来逃脱? What is the best way ? 什么是最好的方法 ?

jQuery .html() automatically escapes things like quotes into their HTML entity form. jQuery .html()自动将引号等内容转义为HTML实体形式。 The HTML actually being put into the DOM looks like this: 实际上放入DOM中的HTML如下所示:

{"blocks":[{"frames_content":"<div id="\&quot;

Notice the &quot; 请注意&quot; , which when displayed in a browser will be a double quote. ,当显示在浏览器中时将是双引号。 It's an HTML entity. 这是一个HTML实体。

To pull the code you want back out of the DOM without the HTML entities, use .text() instead of .html() 要在没有HTML实体的情况下从DOM中拉出您想要的代码,请使用.text()而不是.html()

var str=$("#debug").text();

Additionally, I would encourage you to get comfortable debugging your code. 此外,我鼓励您轻松调试代码。 console.log() -ing the contents of the str variable would have helped you uncover what's going on. console.log() str变量的内容将帮助您发现正在发生的事情。

Using the HTML parser and serializer does not return the original string unchanged. 使用HTML解析器和序列化程序不会返回原始字符串不变。

You should call JSON.parse with the unaltered result of JSON.stringify(obj) . 您应该使用JSON.stringify(obj)的不变结果调用JSON.parse

 var obj = { "blocks": [{ "frames_content": "<div id=\\"page\\" class=\\"page\\">\\n \\n \\t<div class=\\"container\\" id=\\"divider1\\">\\n \\n \\t\\t<div class=\\"col-md-12\\">\\n \\n \\t\\t\\t<hr class=\\"dashed\\" data-selector=\\"hr.dashed\\" style=\\"outline: none; cursor: inherit;\\">\\n \\t\\n \\t\\t</div><!-- /.col -->\\n \\t\\n \\t</div><!-- /.container -->\\n \\n </div>", "frames_sandbox": false, "frames_loaderFunction": "", "frames_height": 162, "frames_original_url": "/static/admin-users/pages/elements/divider1.html" }] }; var str = JSON.stringify(obj); document.getElementById('debug').innerHTML = str; console.log(JSON.parse(str)); 
 <div id="debug"></div> 

Please use: 请用:

var str=$("#debug").text(); // not html() 

This will fix your issue with deserializing JSON. 这将解决反序列化JSON的问题。

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

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