[JS] processing JSON parsing failure due to string wrapping

Source: Internet
Author: User

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&nbsp ;</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&nbsp ;</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&nbsp;</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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.