简体   繁体   中英

convert string to JSON object throws Unexpected token

i am trying to convert a string with escape characters to JSON but get error

  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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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