Special characters in the processing of JSON data

Source: Internet
Author: User
Tags control characters

JSON is now a non-common way to process data. But because you are using reflection to get the data, you must handle the special characters yourself, but always find some unseen characters in the foreground

var obj = Jquery.parsejson (msg); conversion failed.

For example, the following can see only two characters in VS

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvehvleglhb2rvbmcwmdk=/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma== /dissolve/70/gravity/southeast "/>

Can actually have three characters, open with notepad++


Has not been clear how these characters entered the database, but entered, it must be processed, or in the foreground will be converted failure. Causes data load to fail.

For a very long time, I thought it was because the user copied some other encoded text, and there was no corresponding encoding in Chinese. The resulting error. This time it came again. So he studied it carefully. It turns out to be the control character in the Ascⅱ table.

It is very easy to be clear about this process naturally.

The code for background reflection into 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 ("attribute not present" + pname); }} protected static string convertfrommodetojson<t> (T T, string Columninfos) where T:class {Stri ng[] 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>//&L t;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 the Ascⅱ code. Numbers No. 0 to 31st and 127th (33 total) are control characters or communication private characters {} else {sb. Append(c); } break; }} return sb. ToString (); }


Control characters

Special characters in the processing of 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.