Special character handling in JSON data

Source: Internet
Author: User

Today in the project encountered a problem, the data on the page suddenly show no, after the inspection of the JSON data is a problem. When using JSON to transfer data from the background to the foreground, when the data itself contains some special characters, the parsing of the JSON data can be faulted. If the content itself contains "\" "double quotes," \ r \ n "Carriage return line, the parsing of the data will be problematic.

The difference between \r,\n,\r\n

\ n is a newline, English is new line, which means that the cursor to the beginning of the start \ R is the carriage return, English is carriage return, the cursor is moved down a grid \ r \ n indicates carriage return line

The difference between "\ r \ n" and "</br>"

\ r \ n is the output of the HTML code wrapping (when viewing the HTML code, the code is wrapped) <br/> Output to the browser wrapping (when viewing the page effect, text wrapping)

When the user enters the content in the textarea, sometimes enters the double quotation mark, the carriage return or the newline character, when saves, these special symbols also also the text content saves in the database, when obtains this data, the JSON parsing will be wrong

The following are problematic data:

{"Employees": [{"FirstName": "Bill", "LastName": "Gates" "},{" FirstName ":" George Returns "," LastName ":" Bush "},{" FirstName " ": Thomas", "LastName": "Carter"}]}

  

How can you upload data to a page without altering the data? The idea is the back end of the \ r \ n (carriage return) to <br/>, front-end and then <br/> back to \ r \ n

C # code:

1  Public Static stringEncodetextareachar (stringstr)2         {3             if(str = =NULL)return NULL;4             returnStr. Replace ("\"","\\\""). Replace ("\ r \ n","<br/>"). Replace ("\ n","<br/>"). Replace ("\ r","<br/>");5}

JavaScript code

1 function Encodetextarea (str) {2         str = str.replace (/&lt;/g, "<"). Replace (/&gt;/g, ">"  ); 3         var str = str.replace (/<br\/>/g, "\ r \ n"); 4         return str; 5     }

Both languages have the replace () method, and they are slightly different,

The replace of JavaScript is replaced only once, such as "ABCAEBACD" has two C, it only replaces the first, replace ("C", "s"), the result is "ABSAEBACD"

Replace All of the C # replacement ("C", "s"), and the result is "ABSAEBASD"

Note: For Javascript to implement all substitutions, you can replace (/c/g, "s"),//between the contents to be replaced, and g as the global flag

Why do you want to add this one? Str.replace (/&lt;/g, "<"). Replace (/&gt;/g, ">"), because the back end of the    <br/>    becomes the &lt;br/&gt ;

  

Special character handling in JSON data

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.