[英]convert string to JSON object throws Unexpected token
i am trying to convert a string with escape characters to JSON but get error 我正在尝试将带有转义字符的字符串转换为JSON但出现错误
var str = '[{"name":"content","readonly":false,"value":"<div class=\"blueheading\"><h2>Free Gifts for Him</h2><h3><a href=\"http://abcd.com/free-gifts-with-purchase-for-him.aspx\">View all for Him</a></h3></div>"},{"name":"cached","readonly":true,"value":true}]';
var myObj = JSON.parse(str);
If you want to have \\"
in a string literal, you have to escape the \\
: '\\\\"'
. 如果要在字符串文字中使用\\"
,则必须转义\\
: '\\\\"'
。
This gives : 这给出了:
var str = '[{"name":"content","readonly":false,"value":"<div class=\\"blueheading\\"><h2>Free Gifts for Him</h2><h3><a href=\\"http://abcd.com/free-gifts-with-purchase-for-him.aspx\\">View all for Him</a></h3></div>"},{"name":"cached","readonly":true,"value":true}]';
var myObj = JSON.parse(str);
Read more about special characters in string literals 阅读有关字符串文字中特殊字符的更多信息
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.