An error occurred while converting the json string to a json object due to special characters.

Source: Internet
Author: User

If the data obtained from the database (especially the description information) contains special characters, JSON is used. an error occurs when parse converts a json string to a json object, mainly because of double quotation marks and carriage return. The left angle brackets and right angle brackets also cause display problems, therefore, some special characters must be encoded or escaped before the json object is converted to the output page. The following shows the C # code encoding and escape common special characters. After the author tests and encodes and escapes these symbols, most json strings can be converted into json objects. If you encounter some problems, you should look for the problem in this direction.

TheString = theString. replace (">", "& gt;"); theString = theString. replace ("<", "& lt;"); theString = theString. replace ("", "& nbsp;"); theString = theString. replace ("\" "," & quot; "); theString = theString. replace ("\ '", "& #39;"); theString = theString. replace ("\ n", "\ n"); theString = theString. replace ("\ r", "\ r ");

Note: \ r returns to the beginning of the line, and \ n is the new line. Both of them appear at the same time and should be processed at the same time.


Supplement: line breaks in the middle of the text. spaces are not displayed in the database in the form of \ r \ n, & nbsp; (line breaks between "book" and "first 80, space between "by" and "Cao Xueqin)

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.