Error _javascript technique when converting a JSON string into a JSON object using Json.parse

Source: Internet
Author: User

In the case of data extracted from the database (especially descriptive information) containing special characters, There is an error when converting a JSON string into a JSON object using Json.parse. It is mainly double quotes, carriage return, and so on, and left angle brackets and right angle brackets can also cause problems, so to encode or escape some special conformance before outputting to the page for JSON object conversion, the following shows the C #代码编码和转义几个常用特殊字符. After the author tests, after encoding and escaping these symbols, most of the JSON strings can be converted to JSON objects. If you encounter individual problems, you should look for the problem in this direction.

TheString = Thestring.replace (">", ">"); 
TheString = Thestring.replace ("<", "<"); 
TheString = Thestring.replace ("", ""); 
TheString = Thestring.replace ("\" "," ""); 
TheString = Thestring.replace ("\", "'"); 
TheString = Thestring.replace ("\", "\\\\");//The Escape of the slash 
thestring = thestring.replace ("\ n", "\\n"); 
TheString = Thestring.replace ("\ r", \ r);

Note: \ r is back at the beginning, \ n is the Le line, these two generally appear simultaneously and should be processed at the same time.

Add: Text in the middle of the line, the space in the database is not to \ r \ n, etc form display ("book" and "Top 80" between the line, "from" and "Cao Xueqin" space)

Text:

Database:

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.