zoukankan      html  css  js  c++  java
  • 包含时间的Json序列化

     public static string ObjectToJson<T>(T t)
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T));
                    serializer.WriteObject(ms, t);
                    ms.Position = 0;
                    using (StreamReader reader = new StreamReader(ms))
                    {
                        string json = reader.ReadToEnd();
                        string p = @"\/Date((d+)+d+)\/";
                        MatchEvaluator evaluator = new MatchEvaluator(ConvertJsonDataToDataString);
                        Regex reg = new Regex(p);
                        json = reg.Replace(json, evaluator);
                        return json;
                    }
                }
    
            }
    
            public static T JsonToObject<T>(string json)
            {
                string p = @"d{4}-d{2}-d{2}sd{2}:d{2}:d{2}";
                MatchEvaluator evaluator = new MatchEvaluator(ConvertDateStringToJsonDate);    //对时间进行处理
                Regex reg = new Regex(p);
                json = reg.Replace(json, evaluator);
                using (MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(json)))
                {
                    DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T));
                    T data = (T)serializer.ReadObject(ms);
                    return data;
                }
            }
    
            /// <summary>
            /// 将Json时间转换为时间字符
            /// </summary>
            /// <param name="m"></param>
            /// <returns></returns>
            private static string ConvertJsonDataToDataString(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>
            /// 将时间字符转换成JSON时间
            /// </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;
            }
     [DataContract]
        public class RecentVisitors
        {
            [DataMember]
            public long UserId { get; set; }
            [DataMember]
            public string UserName { get; set; }
            [DataMember]
            public DateTime Dt { get; set; }
        }
    var jsonStr="[{"UserId":"123123","UserName":"小小苏","Dt":"2012-09-25 10:50:00"},{"UserId":"456456","UserName":"大头","Dt":"2012-09-25 10:50:00"}]";
    调用:
    1:JsonToObject<List<RecentVisitors>>(jsonStr)
    2:ObjectToJson<List<RecentVisitors>>(new List<RecentVisitors);
  • 相关阅读:
    IOS使用 swizzle 解决一些错误
    Objective-C的hook方案(一): Method Swizzling
    jmeter录制Chrome浏览器https请求进行压力测试
    FIDDLER导出JMX文件,JMETER打开导出的JMX报错的解决方式
    Fiddler的PC端与手机端抓包配置步骤
    初识中间件之消息队列--提高服务性能
    Python虚拟环境配置应用
    jmeter三种阶梯式加压
    JMETER-正则表达式提取与查看变量是否提取正确
    jmeter的线程数,并发用户数,TPS,RPS 关系解说
  • 原文地址:https://www.cnblogs.com/wzq806341010/p/3311779.html
Copyright © 2011-2022 走看看