C# JSON 转List<T>类
来源:互联网 发布:win10 软件桌面图标 编辑:程序博客网 时间:2024/05/16 17:08
因为项目需要,形成一个JSON帮助类
这是在.net3.5的环境下编写
除了要添加引用
System.Runtime.Serialization;
还要添加
System.ServiceModel
System.ServiceModel.Web
public class JsonHelper { /// <summary> /// 把Json转成List<T> /// </summary> /// <typeparam name="T"></typeparam> /// <param name="html"></param> /// <returns></returns> public static IList<T> JsonToList<T>(string html) { IList<T> result = new List<T>(); html = FormatJson(html); try { DataContractJsonSerializer _Json = new DataContractJsonSerializer(result.GetType()); byte[] _Using = System.Text.Encoding.UTF8.GetBytes(html); System.IO.MemoryStream _MemoryStream = new System.IO.MemoryStream(_Using); _MemoryStream.Position = 0; object obj = _Json.ReadObject(_MemoryStream); ; result = (IList<T>)obj; } catch (Exception) { html = AttributeToVariable(html); DataContractJsonSerializer _Json = new DataContractJsonSerializer(result.GetType()); byte[] _Using = System.Text.Encoding.UTF8.GetBytes(html); System.IO.MemoryStream _MemoryStream = new System.IO.MemoryStream(_Using); _MemoryStream.Position = 0; object obj = _Json.ReadObject(_MemoryStream); ; result = (IList<T>)obj; } return result; } #region // 格式化Json字符串 /// <summary> /// 格式化Json字符串,使之能转换成List /// </summary> /// <param name="html"></param> /// <returns></returns> public static string FormatJson(string value) { string p = @"(new Date)\(+([0-9,-]+)+(\))"; MatchEvaluator matchEvaluator = new MatchEvaluator(FormatJsonMatch); Regex reg = new Regex(p); bool isfind = reg.IsMatch(value); value = reg.Replace(value, matchEvaluator); return value; } /// <summary> /// 将Json序列化的时间由new Date(1373387734703)转为字符串"\/Date(1373387734703)\/" /// </summary> private static string FormatJsonMatch(Match m) { return string.Format("\"\\/Date({0})\\/\"", m.Groups[2].Value); } #endregion // 格式化Json字符串 #region // 格式化日期 /// <summary> /// 将Json序列化的时间由new Date(1373390933250) 或Date(1373390933250)或"\/Date(1373390933250+0800)\/" /// 转为2013-07-09 17:28:53 /// 主要用于传给前台 /// </summary> /// <param name="html"></param> /// <returns></returns> public static string FormatJsonDate(string value) { string p = @"(new Date)\(+([0-9,-]+)+(\))"; MatchEvaluator matchEvaluator = new MatchEvaluator(FormatJsonDateMatch); Regex reg = new Regex(p); value = reg.Replace(value, matchEvaluator); p = @"(Date)\(+([0-9,-]+)+(\))"; matchEvaluator = new MatchEvaluator(FormatJsonDateMatch); reg = new Regex(p); value = reg.Replace(value, matchEvaluator); p = "\"\\\\\\/" + @"Date(\()([0-9,-]+)([+])([0-9,-]+)(\))" + "\\\\\\/\""; matchEvaluator = new MatchEvaluator(FormatJsonDateMatch); reg = new Regex(p); value = reg.Replace(value, matchEvaluator); return value; } /// <summary> /// 将Json序列化的时间由Date(1294499956278+0800)转为字符串 /// </summary> private static string FormatJsonDateMatch(Match m) { string result = string.Empty; DateTime dt = new DateTime(1970, 1, 1); dt = dt.AddMilliseconds(long.Parse(m.Groups[2].Value)); dt = dt.ToLocalTime(); result = dt.ToString("yyyy-MM-dd HH:mm:ss"); return result; } #endregion // 格式化日期 #region // 属性和变量转换 /// <summary> /// 属性转变量 /// 把"<address>k__BackingField":"1", /// 转成"address":"1", /// </summary> /// <param name="value"></param> /// <returns></returns> public static string AttributeToVariable(string value) { string p = @"\<([A-Z,a-z,0-9,_]*)\>k__BackingField"; MatchEvaluator matchEvaluator = new MatchEvaluator(AttributeToVariableMatch); Regex reg = new Regex(p); bool isfind = reg.IsMatch(value); value = reg.Replace(value, matchEvaluator); return value; } private static string AttributeToVariableMatch(Match m) { return m.Groups[1].Value; } /// <summary> /// 变量转属性 /// 把"address":"1", /// 转成"<address>k__BackingField":"1", /// </summary> /// <param name="value"></param> /// <returns></returns> public static string VariableToAttribute(string value) { string p = "\\\"([A-Z,a-z,0-9,_]*)\\\"\\:"; MatchEvaluator matchEvaluator = new MatchEvaluator(VariableToAttributeMatch); Regex reg = new Regex(p); bool isfind = reg.IsMatch(value); value = reg.Replace(value, matchEvaluator); return value; } private static string VariableToAttributeMatch(Match m) { return string.Format("\"<{0}>k__BackingField\":", m.Groups[1].Value); } #endregion // 属性和变量转换 }
- C# JSON 转List<T>类
- c# Json转 List<T>
- JSON之Asp.net MVC C#对象转JSON,DataTable转JSON,List<T>转JSON,JSON转List<T>,JSON转C#对象
- JSON之Asp.net MVC C#对象转JSON,DataTable转JSON,List<T>转JSON,JSON转List<T>,JSON转C#对象
- C# List<T>转为Json数据
- C# List转Json
- c# Json List<T> DataTable 数据相互转换
- c# Json List<T> DataTable 数据相互转换
- c# Json List<T> DataTable 数据相互转换
- c# Json List<T> DataTable 数据相互转换
- c# Json List<T> DataTable 数据相互转换
- c# Json List<T> DataTable 数据相互转换
- C#中List(Of T)类
- C# List<T>用法
- C# List<T>用法
- C# List<T>用法
- C# List<T>用法
- C# List<T>用法
- 电路设计——电容设计
- Kindle刚入华,电子书瞬间被破解!
- Data Structures (Weiss) Chapter 4: Binary Search Tree C++
- 线段树——HDU1255 覆盖的面积解题报告
- 一道有意思的acm: 1789. Searching for the Dodecahedron
- C# JSON 转List<T>类
- 为公司做好长远计划company plan ahead
- DevSuite项目管理平台助力莱芜钢铁集团,有效管控并行项目
- zoj 1095 丑数、1151反转单词、1088 约瑟夫环
- hud 2151 Worm
- Data Structures (Weiss) Chapter 4: AVL Trees 平衡树
- ffmpeg裁剪合并视频
- Html----textarea
- Caused by: java.lang.InstantiationException