C # convert JSON to List & lt; T & gt; Class

Source: Internet
Author: User

A json help class is formed as required by the project.

This is written in the. net3.5 Environment

 

Public class JsonHelper {// <summary> // convert Json to List <T> /// </summary> /// <typeparam name = "T"> </ typeparam> // <param name = "html"> </param> // <returns> </returns> public static IList <T> JsonToList <T> (string html) {IList <T> result = new List <T> (); html = FormatJson (html); try {DataContractJsonSerializer _ Json = new DataContractJsonSerializer (result. getType (); byte [] _ Using = System. text. encoding. UTF8.GetBytes (html); System. IO. memoryStream _ MemoryStream = new System. IO. memoryStream (_ Using); _ MemoryStream. position = 0; object obj = _ Json. readObject (_ MemoryStream); result = (IList <T>) obj;} catch (Exception) {html = AttributeToVariable (html); DataContractJsonSerializer _ Json = new DataContractJsonSerializer (result. getType (); byte [] _ Using = System. text. encoding. UTF8.GetBytes (html); System. IO. memoryStream _ MemoryStream = new System. IO. memoryStream (_ Using); _ MemoryStream. position = 0; object obj = _ Json. readObject (_ MemoryStream); result = (IList <T>) obj;} return result ;} # region // format the Json string /// <summary> // format the Json string, convert it to List /// </summary> /// <param name = "html"> </param> /// <returns> </returns> public static string formatJson (string value) {string p = @ "(new Date) \ (+ ([0-9,-] +) + (\)"; MatchEvaluator matchEvaluator = new MatchEvaluator (FormatJsonMatch ); regex reg = new Regex (p); bool isfind = reg. isMatch (value); value = reg. replace (value, matchEvaluator); return value ;}/// <summary> // convert the Json serialization time from new Date (1373387734703) to the string "\/Date (1373387734703) \/"// </summary> private static string FormatJsonMatch (Match m) {return string. format ("\" \/Date ({0}) \/\ "", m. groups [2]. value) ;}# endregion // format the Json string # region // format the Date /// <summary> // serialize the time of Json from new Date (1373390933250) or Date (1373390933250) or "\/Date (1373390933250 + 0800) \/"// converted to 17:28:53 // It is mainly used for sending to the front-end // </summary> /// <param name =" html "> </param> // /<returns> </returns> public static string FormatJsonDate (string value) {string p = @ "(new Date) \ (+ ([0-9,-] +) + (\)"; MatchEvaluator matchEvaluator = new MatchEvaluator (FormatJsonDateMatch ); regex reg = new Regex (p); value = reg. replace (value, matchEvaluator); p = @ "(Date) \ (+ ([0-9,-] +) + (\))"; matchEvaluator = new MatchEvaluator (FormatJsonDateMatch); reg = new Regex (p); value = reg. replace (value, matchEvaluator); p = "\" \\\\\\/ "+ @" Date (\ () ([0-9,-] +) ([+]) ([0-9,-] +) (\) "+ "\\\\\\/\""; matchEvaluator = new MatchEvaluator (FormatJsonDateMatch); reg = new Regex (p); value = reg. replace (value, matchEvaluator); return value ;}/// <summary> // serialize the Json time from Date (1294499956278 + 0800) convert to string /// </summary> private static string FormatJsonDateMatch (Match m) {string result = string. empty; DateTime dt = new DateTime (1970, 1, 1); dt = dt. addMilliseconds (long. parse (m. groups [2]. value); dt = dt. toLocalTime (); result = dt. toString ("yyyy-MM-dd HH: mm: ss"); return result ;} # endregion // format the date # region // conversion of attributes and variables /// <summary> // conversion of attributes // set "<address> k _ BackingField ": "1", // convert to "address": "1 ", /// </summary> /// <param name = "value"> </param> /// <returns> </returns> public static string AttributeToVariable (string value) {string p = @ "\ <([A-Z, a-z, 0-9, _] *) \> k _ BackingField "; matchEvaluator matchEvaluator = new MatchEvaluator (AttributeToVariableMatch); Regex reg = new Regex (p); bool isfind = reg. isMatch (value); value = reg. replace (value, matchEvaluator); return value;} private static string AttributeToVariableMatch (Match m) {return m. groups [1]. value;} // <summary> // convert the variable to a property. // convert "address": "1 ", /// convert to "<address> k _ BackingField": "1 ", /// </summary> /// <param name = "value"> </param> /// <returns> </returns> public static string VariableToAttribute (string value) {string p = "\" ([A-Z, a-z, 0-9, _] *) \ "\:"; matchEvaluator matchEvaluator = new MatchEvaluator (VariableToAttributeMatch); Regex reg = new Regex (p); bool isfind = reg. isMatch (value); value = reg. replace (value, matchEvaluator); return value;} private static string VariableToAttributeMatch (Match m) {return string. format ("\" <{0}> k _ BackingField \ ":", m. groups [1]. value) ;}# endregion // convert attributes and variables}

 

Related Article

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.