Requirements: Remove the string from a database field, to JSON, the results found that the error is resolved failure, found that the string is removed because of the result of wrapping, now need to replace the string inside the line with ", so that the string can be successfully parsed into a JSON object.
Technology: Rely on the Replace function to implement
----------------------------------------
Inside the database.
----------------------------------------
{"ChoiceList": {"A": "<p><span>spare</span></p>", "B": "<p><span>fast  ;</span></p> "," C ":" <p><span>moderate</span></p> "," D ":" <p><span >moral</span></p> "}}
----------------------------------------
The appearance of removing extra space
----------------------------------------
{"ChoiceList": {"A": "<p><span>spare</span></p>", "B": "<p><span>fast  ;</span></p> "," C ":" <p><span>moderate</span></p> "," D ":" <p><span >moral</span></p> "}}
----------------------------------------
The final resolution is successful.
----------------------------------------
{ "choicelist": { "A": "<p><span>spare</span></p>", "B": "<p><span>fast </span></p>", "C": "<p><span>moderate </span></p> ", " D ":" <p><span>moral</span></p> " }}
Key code:
var json= exception string
JSON = Json.replace (/\n/g, ""). Replace (/\r/g, "");//Remove line breaks from string
JSON = Json.replace (/\n/g, ""). Replace (/\s|\xa0/g, "");//Remove all spaces in the string
var jsonobj= eval (' (' + JSON + ') ');//Parse a string into a JSON object
Console.log (Jsonobj);
Test:
http//Www.w3school.com.cn/tiy/t.asp?f=jquery_hide<! DOCTYPE html>$ (document). Ready (function() {varJson= ' {"choicelist": {"A2": "<p>\n\n<span> spare</span>\n\n</p>", "B": "&L T;p>\n\n<span>fast </span>\n\n</p> "," C ":" <p>\n\n<span> moderate< /span>\n\n</p> "," D ":" <p>\n\n<span>moral</span>\n\n</p> "}} '; JSON= Json.replace (/\n/g, ""). Replace (/\r/g, ""). Replace (/\s|\xa0/g, ""); varjsonobj= eval (' (' + JSON + ') '); Console.log (Jsonobj); });</script>[JS] processing JSON parsing failure due to string wrapping