C # JSON generic Conversion

Source: Internet
Author: User
Using system; using system. collections. generic; using system. web; using system. runtime. serialization. JSON; using system. LINQ; using system. text; using system. io; using system. text. regularexpressions; namespace jsonhelper {public class jsonclass {// <summary> // JSON serialization /// </Summary> /// <typeparam name = "T"> Object Type </ typeparam> // <Param name = "T"> serialized object </param> /// <returns> </returns> Public static string jsonserializer <t> (T) {datacontractjsonserializer SER = new datacontractjsonserializer (typeof (t); memorystream MS = new memorystream (); Ser. writeobject (MS, T); string jsonstring = encoding. utf8.getstring (Ms. toarray (); Ms. close (); string P = @ "\/date \ (\ D +) \ + \ D + \)\\/"; matchevaluator = new matchevaluator (convertjsondatetodatestring); RegEx Reg = new RegEx (p); jsonstring = reg. replace (jsonstring, matchevaluator); Return jsonstring ;} /// <summary> /// JSON deserialization /// </Summary> /// <typeparam name = "T"> Object Type </typeparam> /// <param name = "jsonstring"> deserializing a json string </param> // <returns> </returns> Public static t jsondeserialize <t> (string jsonstring) {string P = @ "\ D {4}-\ D {2}-\ D {2} \ s \ D {2 }:\ d {2 }: \ D {2} "; matchevaluator = new matchevaluator (convertdatestringtojsondate); RegEx Reg = new RegEx (p); jsonstring = reg. replace (jsonstring, matchevaluator); datacontractjsonserializer SER = new datacontractjsonserializer (typeof (t); memorystream MS = new memorystream (encoding. utf8.getbytes (jsonstring); t OBJ = (t) Ser. readobject (MS); Return OBJ;} Private Static string convertjsondatetodatestring (Match m) {string result = string. empty; datetime dt = new datetime (1970, 1, 1); dt = DT. addmilliseconds (Long. parse (M. groups [1]. value); dt = DT. tolocaltime (); Result = DT. tostring ("yyyy-mm-dd hh: mm: SS"); return result;} Private Static string convertdatestringtojsondate (Match m) {string result = string. empty; datetime dt = datetime. parse (M. groups [0]. value); dt = DT. touniversaltime (); timespan Ts = DT-datetime. parse ("1970-01-01"); Result = string. format ("\/date ({0} + 0800) \/", ts. totalmilliseconds); return result ;}}}

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.