The previous article wrote about the conversion of entity classes in. NET to JSON format, and today, when doing a specific conversion, it was found that the conversion of previous versions of JSONHELP was not comprehensive for date types. In previous versions of Jsonhelp, when converting from entity classes to JSON format, the date format was transformed into a timestamp. This jsonhelp has been updated here. To resolve the issue of converting date type fields. The code is as follows:
JsonHelp.cs
Using system;using system.collections.generic;using system.linq;using system.text;using System.Threading.Tasks; Using system.runtime.serialization.json;using system.io;using system.text.regularexpressions;namespace JsonTest{ Class Jsonhelp {public Jsonhelp () {////Todo:add constructor logic here }///<summary>///Serializing the object JSON string///</summary>//<typepara M name= "T" > Object type </typeparam>//<param name= "obj" > Object entity </param>///<returns>json Word String </returns> public static string getjson<t> (T obj) {//Remember to add Reference SYSTEM.SERVICEMODEL.W EB/** * If you do not add the above reference, System.Runtime.Serialization.Json; JSON is not out of the OH * * */DataContractJsonSerializer JSON = new DataContractJsonSerializer (typeof (T)); using (MemoryStream ms = new MemoryStream ()) {json. WRiteobject (MS, obj); String jsonstring = Encoding.UTF8.GetString (ms. ToArray ()); String pattern = @ "\\/date\ ((\d+) \+\d+\) \\/"; MatchEvaluator matchevaluator = new MatchEvaluator (convertjsondatetodatestring); Regex reg = new regex (pattern); jsonstring = Reg. Replace (jsonstring, MatchEvaluator); return jsonstring; }}///<summary>//To restore the JSON string to an object///</summary>//<typeparam name= " T "> Object type </typeparam>//<param name=" jsonstring ">json string </param>//<returns> Object Real Body </returns> public static T parseformjson<t> (string jsonstring) {T obj = activator.c Reateinstance<t> (); String pattern = @ "\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}"; MatchEvaluator matchevaluator = new MatchEvaluator (convertdatestringtojsondate); Regex reg = new Regex (pAttern); jsonstring = Reg. Replace (jsonstring, MatchEvaluator); using (MemoryStream ms = new MemoryStream (Encoding.UTF8.GetBytes (jsonstring))) {DATACONTRACTJSO Nserializer DCJ = new DataContractJsonSerializer (typeof (T)); Return (T) DCJ. ReadObject (MS); }}///<summary>//Convert JSON to serialized time by/date (1304931520336+0800)/Convert to string////</summary> 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; }///<summary>//Convert time string to JSON time///</summary> private static string Convertdat Estringtojsondate (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; } }}
Test code:
Using consoleapplication1.models;using system;using system.collections.generic;using System.Linq;using System.Text; Using System.threading.tasks;namespace consoleapplication1{ class program { static void Main (string[] args) { using (var db = new Joowmscontext ()) { var admin = db. Admins.where (p = p.id > 0). (p = p.id); foreach (var ad in admin) { Console.WriteLine (jsontest.jsonhelp.getjson< Consoleapplication1.models.admin> (AD)); } Console.readkey ();}}}
EF is used when writing test code, and a good tool is recommended here. EF Power Tools is currently a beta4 version.
. NET entity classes and JSON transformations (. NET self-implemented class library implementation) updates