JsonHelper类(序列化和反序列化辅助类)

   1: using System;
   2: using System.Collections.Generic;
   3: using System.Linq;
   4: using System.Web;
   5: using System.Runtime.Serialization.Json;
   6: using System.IO;
   7: using System.Text;
   8: using System.Text.RegularExpressions;
   9:  
  10: /// <summary>
  11: /// JSON序列化和反序列化辅助类
  12: /// </summary>
  13: public class JsonHelper
  14: {
  15:     /// <summary>
  16:     /// JSON序列化
  17:     /// </summary>
  18:     public static string JsonSerializer<T>(T t)
  19:     {
  20:         DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
  21:         MemoryStream ms = new MemoryStream();
  22:         ser.WriteObject(ms, t);
  23:         string jsonString = Encoding.UTF8.GetString(ms.ToArray());
  24:         ms.Close();
  25:         //替换Json的Date字符串
  26:         string p = @"\\/Date\((\d+)\+\d+\)\\/";
  27:         MatchEvaluator matchEvaluator = new MatchEvaluator(ConvertJsonDateToDateString);
  28:          Regex reg = new Regex(p);
  29:         jsonString = reg.Replace(jsonString, matchEvaluator);
  30:         return jsonString;
  31:     }
  32:  
  33:     /// <summary>
  34:     /// JSON反序列化
  35:     /// </summary>
  36:     public static T JsonDeserialize<T>(string jsonString)
  37:     {
  38:         //将"yyyy-MM-dd HH:mm:ss"格式的字符串转为"\/Date(1294499956278+0800)\/"格式
  39:         string p = @"\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}";
  40:         MatchEvaluator matchEvaluator = new MatchEvaluator(ConvertDateStringToJsonDate);
  41:         Regex reg = new Regex(p);
  42:         jsonString = reg.Replace(jsonString, matchEvaluator);
  43:         DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
  44:         MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString));
  45:         T obj = (T)ser.ReadObject(ms);
  46:         return obj;
  47:     }
  48:  
  49:     /// <summary>
  50:     /// 将Json序列化的时间由/Date(1294499956278+0800)转为字符串
  51:     /// </summary>
  52:     private static string ConvertJsonDateToDateString(Match m)
  53:     {
  54:         string result = string.Empty;
  55:         DateTime dt = new DateTime(1970,1,1);
  56:         dt = dt.AddMilliseconds(long.Parse(m.Groups[1].Value));
  57:         dt = dt.ToLocalTime();
  58:         result = dt.ToString("yyyy-MM-dd HH:mm:ss");
  59:         return result;
  60:     }
  61:  
  62:     /// <summary>
  63:     /// 将时间字符串转为Json时间
  64:     /// </summary>
  65:     private static string ConvertDateStringToJsonDate(Match m)
  66:     {
  67:         string result = string.Empty;
  68:         DateTime dt = DateTime.Parse(m.Groups[0].Value);
  69:         dt = dt.ToUniversalTime();
  70:         TimeSpan ts = dt - DateTime.Parse("1970-01-01");
  71:         result = string.Format("\\/Date({0}+0800)\\/",ts.TotalMilliseconds);
  72:         return result;
  73:     }
  74: }

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。