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
1. Direct substitution with strings
650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;padding:0px;border:none; "/>
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&NBsp HH:mm:ss "); }); Response.Write (str);//{"Id": 1, "Dt": "2011-08-17 17:38:47"}
650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;padding:0px;border:none; "/>
2.Jsonhelper
650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;padding:0px;border:none; "/>
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 classes /// </summary> Public class jsonhelper { /// <summary > /// json Serialization /// </ Summary> public static string jsonserializer<t > (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 (); //Replace the JSON date string 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> (string jsonstring) { //Convert the string "Yyyy-mm-dd hh:mm:ss" 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); &nBsp; 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> 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 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; } &nbsP;}
650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;padding:0px;border:none; "/>
List<t> Serialization:
1:list<person> List = new List<person> ()
2: {
3:new person () {name= "Zhang San", age=28},
4:new person () {name= "John Doe", age=25}
5:};
6:
7:string jsonstring = jsonhelper.jsonserializer<list<person>> (List);
Serialization results:
"[{/" age/": 28,/" name/":/" Zhang San/"},{/" age/": 25,/" name/":/" John Doe/"}]"
dictionaries cannot be directly used for json,dictionary dictionaries to be converted to JSON instead of the original dictionary format, but in the form of dictionary key as the value of the name "key", with dictionary value as the name " Value ". Such as:
1:dictionary<string, string> dic = new dictionary<string, string> ();
2:dic. ADD ("Name", "Zhang San");
3:dic. ADD ("Age", "28");
4:
5:string jsonstring = Jsonhelper.jsonserializer < Dictionary<string, string>> (DIC);
Serialization results:
1: "[{/" key/":/" name/",/" value/":/" Zhang San/"},{/" key/":/" age/",/" value/":/" 28/"}]"
JavaScriptSerializer serialization of JSON time format