承接上篇,現在我在類中加入一個日期時間類型的欄位:
public class Student { public string StudentId { get; set; } public string Name { get; set; } public int age { get; set; } public string Address { get; set; } public DateTime Birthday { get; set; } }
如果不對birthday這個類型處理的話,序列化之後就會出現這個問題:
{"Address":"北京朝陽區","Birthday":"\/Date(1434943392228+0800)\/","Name":"水田如雅","StudentId":"110","age":20}
現在我們在原來的方法上面進行一個改進:
public static class JsonConvert { /// <summary> /// Converts the obj to json. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="t">The t.</param> /// <returns>System.String.</returns> /// <remarks>Editor:v-liuhch CreateTime:2015/6/21 21:40:55</remarks> public static string ConvertObjToJson<T>(T t) { DataContractJsonSerializer ser = new DataContractJsonSerializer(t.GetType()); try { using (MemoryStream ms=new MemoryStream()) { ser.WriteObject(ms,t); string strJson=Encoding.UTF8.GetString(ms.ToArray()); //替換Json的date字串 string p = @"\\/Date\((\d+)\+\d+\)\\/"; MatchEvaluator matchEvaluator = new MatchEvaluator(ConvertJsonDataToDateString); Regex reg = new Regex(p); strJson = reg.Replace(strJson, matchEvaluator); return strJson; } } catch (IOException) { //自己處理異常吧 return null; } } /// <summary> /// Jsons the deserialize. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="strJson">The STR json.</param> /// <returns>``0.</returns> /// <remarks>Editor:v-liuhch CreateTime:2015/6/21 21:46:37</remarks> public static T JsonDeserialize<T>(string strJson) where T:class //約束T為class,也可以放寬類型,這裡只是個例子,寫著玩兒的,歐巴們,此處可改 { DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T)); try { //將"yyyy-MM-dd HH:mm:ss"格式的字串轉為"\/Date(1294499956278+0800)\/"格式 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); strJson = reg.Replace(strJson, matchEvaluator); using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(strJson))) { T obj = ser.ReadObject(ms) as T; return obj; } } catch (IOException e) { //自己處理異常吧 return null; } } /// <summary> /// Converts the json data to date string. /// </summary> /// <param name="m">The m.</param> /// <returns>System.String.</returns> /// <remarks>Editor:v-liuhch CreateTime:2015/6/22 11:39:18</remarks> private static string ConvertJsonDataToDateString(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> /// Converts the date string to json date. /// </summary> /// <param name="m">The m.</param> /// <returns>System.String.</returns> /// <remarks>Editor:v-liuhch CreateTime:2015/6/22 11:39:15</remarks> 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; } }
當然,在序列化的時候,我們可以不對日期時間做單獨處理,拿到前台JS之後,再對資料進行一個整合過濾,這樣,就不會固定死日期時間的返回格式,感覺這樣比在幕後處理好。
以上就是.net——序列化與還原序列化中對日期時間的處理的內容,更多相關內容請關注topic.alibabacloud.com(www.php.cn)!