Using JavaScriptSerializer to serialize the JSON time format, the resulting datetime value is displayed as a JSON string in the form of "/date (700000+0500)/", apparently to be converted
\/date (1404098342309)/, after analysis, this 1404098342309 value is the total number of milliseconds that is the difference between the January 1, 1970 (the minimum value of datetime) and the date actually represented by dates.
1. Direct substitution with strings
Model M = new Model {Id = 1, Dt = DateTime.Now}; JavaScriptSerializer js = new JavaScriptSerializer (); String str = js. Serialize (m); str = regex.replace (str, @ "\\/date\ ((\d+) \) \\/", match = = { datetime dt = new DateTime (1970, 1, 1); DT = dt. Addmilliseconds (Long. Parse (match. GROUPS[1]. Value)); DT = dt. ToLocalTime (); Return dt. ToString ("Yyyy-mm-dd HH:mm:ss"); });
2.JsonHelper
Using system;using system.collections.generic;using system.linq;using system.web; Using System.Runtime.Serialization.Json; Using System.IO; Using System.Text; Using System.Text.RegularExpressions; <summary>///JSON serialization and deserialization helper class///</summary> public class Jsonhelper {//<summary> JSON serialization///</summary> public static string jsonserializer<t> (T t) {Datacontra Ctjsonserializer ser = new DataContractJsonSerializer (typeof (T)); MemoryStream ms = new MemoryStream (); Ser. WriteObject (MS, T); String jsonstring = Encoding.UTF8.GetString (ms. ToArray ()); Ms. Close (); Replace the JSON Date string with string p = @ "///date/((/d+)/+/d+/)///"; /*////date/((([/+/-]/d+) | ( /d+)) [/+/-]/d+/]////*/matchevaluator matchevaluator = new MatchEvaluator (convertjsondatetodatestring); Regex reg = new regex (p); jsonstring = Reg. Replace (jsonstring, MatchevaluatOR); return jsonstring; }///<summary>//JSON deserialization///</summary> public static T jsondeserialize<t> (s Tring jsonstring) {//Convert the string "YYYY-MM-DD HH:mm:ss" format to "//date (1294499956278+0800)//" format string p = @ "/d{4}-/d{2}-/d{2}/s/d{2}:/d{2}:/d{2}"; MatchEvaluator 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; }//<summary>//Convert JSON serialized time from/date (1294499956278+0800) to string///</summary> Priv Ate 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 convertdatestr Ingtojsondate (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; } }
JavaScriptSerializer serialization of JSON time format