Special characters in JSON data processing, special characters in json data

Source: Internet
Author: User
Tags control characters

Special characters in JSON data processing, special characters in json data

JSON is now a common way to process data. However, because you use reflection to obtain data, you must process special characters on your own, but some invisible characters are always found on the front end.

Var obj = jQuery. parseJSON (msg); conversion fails.

For example, in Vs, we can see that there are only two characters

It can actually contain three characters, which can be opened using notepad ++.

I have never understood how these characters entered the database, but they must be processed. Otherwise, the conversion will fail at the front-end, resulting in data loading failure.

For a long time, I thought it was an error because the user copied some other encoded text and there was no corresponding encoding in the text. This time again, so I studied it carefully and found that it was the control character in Table ASC II !!

It is easy to understand the processing.

Code for converting background reflection to JSON:

Private static string getValue <T> (T t, string pname) where T: class {Type type = t. getType (); PropertyInfo pinfo = type. getProperty (pname); if (pinfo! = Null) {object v = pinfo. GetValue (t, null); return v! = Null? V. toString (): "";} else {throw new Exception ("nonexistent attribute" + pname);} protected static string ConvertFromModeTojson <T> (T t, string columnInfos) where T: class {string [] cols = columnInfos. split (new char [] {','}, StringSplitOptions. removeEmptyEntries); System. text. stringBuilder sb = new System. text. stringBuilder (300); string result = "{"; foreach (string col in cols) {string name = "\" {0 }\":\" {1} \ ","; string value = getValue <T> (t, col); value = String2Json (value); sb. append (string. format (name, col, value);} result + = sb. toString (). trimEnd (','); result + = "}"; return result ;} /// <summary> /// filter special characters /// </summary> /// <param name = "s"> </param> /// <returns> </returns> private static string String2Json (String s) {StringBuilder sb = new StringBuilder (); for (int I = 0; I <s. length; I + +) {Char c = s. toCharArray () [I]; switch (c) {case '\ "': sb. append ("\" "); break; case '\': sb. append ("\\\\"); break; case '/': sb. append ("\/"); break; case '\ B': sb. append ("\ B"); break; case '\ F': sb. append ("\ f"); break; case '\ N': sb. append ("\ n"); break; case '\ R': sb. append ("\ r"); break; case '\ t': sb. append ("\ t"); break; default: if (c >=0 & c <= 31) | c = 127) // In ASC Ⅱ code, 0th ~ The 31st and 127th (33 in total) are control characters or communication special characters {} else {sb. append (c);} break;} return sb. toString ();}

Control characters

How to handle special characters in JSON

Because Ajax applications are suitable for Sending plain text to server programs and receiving plain text, it is more desirable to generate text APIs than to generate text, JSON allows you to process local JavaScript objects without worrying about how to express them. XML can also provide similar text benefits, but several existing APIs used to convert JavaScript objects into XML are not as mature as json apis; sometimes, you must exercise caution when creating and processing JavaScript objects to ensure that the processing can work with the selected XML session API. But for JSON, the situation is very different: it can process almost all possible object types, and will return you a very good JSON data representation. Therefore, the biggest value of JSON is that JavaScript can be processed as JavaScript rather than as a data format language. 1. When the carriage return problem is passed in JSON, a carriage return will fail. We can use the regular expression to remove the carriage return: $ str = preg_replace ("'([\ r \ n]) [\ s] +'", "", $ str ); // do not use regular $ str = str_replace ("\ n", "", $ str); the transferred string will be free of carriage returns. By the way, record a PHP filter Script: <? Php // $ document should contain an HTML document. // In this example, the HTML Tag, javascript code //, and blank characters are removed. Some common // HTML objects are converted into corresponding text. $ Search = array ("'<script [^>] *?>. *? </Script> 'si ", // remove javascript" '<[\/\!] *? [^ <>] *?> 'Si ", // remove the HTML Tag" '([\ r \ n]) [\ s] +' ", // remove the blank character

How to read data details with special characters in json format

This is an escape action on the interface server before the value of the word value is processed as json. The values in the field are colon and backslash \ and, add \: \, \ "\ To all the commas so that the client will be correct. The following columns are passed in XML and other formats. The same problem also exists: String s =" a: B, c "; s. replace (":","\:"). replace (",","\,");

Javamessage. replaceAll ("<", "<"). replaceAll (">", "> ");

Html: & changed to & <changed to <> changed to> "changed to" pading: The general interval is 10 PX.

Use spaces instead

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.