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: }

JsonHelper类(序列化和反序列化辅助类)的更多相关文章

  1. C# 实体类序列化与反序列化一 (XmlSerializer)

    /// <summary> /// 实体类序列化的反序列化的类 /// </summary> /// <typeparam name="T">& ...

  2. spring boot添加 LocalDateTime 等 java8 时间类序列化和反序列化的支持

    由于项目将原有的  Date类型的字段改造为 LocalDate,LocalDateTime,LocalTime 类型, 发现  spring  对项目的时间格式无法自动转换,故需手动配置下. 在sp ...

  3. Asp.net中Json的序列化和反序列化(二)

     三.JSON序列化和反序列化日期时间的处理 JSON格式不直接支持日期和时间.DateTime值值显示为“/Date(700000+0500)/”形式的JSON字符串,其中第一个数字(在提供的示例中 ...

  4. Asp.net中Json的序列化和反序列化(一)

    JSON是专门为浏览器中的网页上运行的JavaScript代码而设计的一种数据格式.在网站应用中使用JSON的场景越来越多,本文介绍ASP.NET中JSON的序列化和反序列化,主要对JSON的简单介绍 ...

  5. ASP.NET中JSON的序列化和反序列化

    JSON是专门为浏览器中的网页上运行的JavaScript代码而设计的一种数据格式.在网站应用中使用JSON的场景越来越多,本文介绍 ASP.NET中JSON的序列化和反序列化,主要对JSON的简单介 ...

  6. ASP.NET 中JSON 的序列化和反序列化

    JSON是专门为浏览器中的网页上运行的JavaScript代码而设计的一种数据格式.在网站应用中使用JSON的场景越来越多,本文介绍ASP.NET中JSON的序列化和反序列化,主要对JSON的简单介绍 ...

  7. C# 编写通用的JSON数据进行序列化和反序列化

    注意事项:使用JSON系列化和反系列化,必须要添加引用System.Runtime.Serialization. 1.通用类代码如下: /// <summary>    /// JSON序 ...

  8. ASP.NET中JSON的序列化和反序列化(转)

    JSON是专门为浏览器中的网页上运行的JavaScript代码而设计的一种数据格式.在网站应用中使用JSON的场景越来越多,本文介绍ASP.NET中JSON的序列化和反序列化,主要对JSON的简单介绍 ...

  9. 转:Json序列化和反序列化

    JSON是专门为浏览器中的网页上运行的JavaScript代码而设计的一种数据格式.在网站应用中使用JSON的场景越来越多,本文介绍 ASP.NET中JSON的序列化和反序列化,主要对JSON的简单介 ...

随机推荐

  1. TCP/IP TIME_WAIT状态原理

    原文转载:http://elf8848.iteye.com/blog/1739571 IME_WAIT状态原理 ---------------------------- 通信双方建立TCP连接后,主动 ...

  2. The IAR Archive Tool—iarchive

    The IAR Archive Tool—iarchive—creates and manipulates a library (anarchive) of several ELF object fi ...

  3. PHP网址

    15个魔术方法的总结: http://blog.csdn.net/bossdarcy/article/details/6210794 PHP代码重构:http://blog.csdn.net/tony ...

  4. uva 11324 The Largest Clique(强连通分量缩点+DAG动态规划)

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=25&page=sh ...

  5. jQuery Mobile 手动显示ajax加载器,提示加载中...

    在使用jQuery Mobile开发时,有时候我们需要在请求ajax期间,显示加载提示框(例如:一个旋转图片+一个提示:加载中...).这个时候,我们可以手动显示jQuery Mobile的加载器,大 ...

  6. IOS 7 Study - Manipulating a Navigation Controller’s Array of View

    ProblemYou would like to directly manipulate the array of view controllers associated with aspecific ...

  7. HDU 1695 GCD 容斥

    GCD 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=1695 Description Given 5 integers: a, b, c, d, k ...

  8. cdoj 1141 酱神寻宝 状压dp

    酱神寻宝 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/problem/show/1141 Descri ...

  9. oc-22-sel

    /** sel: 1.作用:包装方法 2.格式:typedef struct objc_selector *SEL; 3.用法: SEL 名称 = @selector(方法); 调用形式: [对象 p ...

  10. 一款基于jQuery多图切换焦点图插件

    这次要给大家分享的也是一款jQuery图片滑块插件,之前有介绍过不少实用的jQuery焦点图插件和jQuery图片滑块插件,比如jQuery左侧Tab切换的图片滑块插件.它的特点是可以同时切换多张图片 ...