Json的使用
Posted hugeboke
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Json的使用相关的知识,希望对你有一定的参考价值。
1 using System; 2 using System.IO; 3 using System.Text; 4 using System.Text.RegularExpressions; 5 using System.Runtime.Serialization.Json; 6 using Newtonsoft.Json; 7 using System.Collections.Generic; 8 9 namespace TravelB2B.Core.Utils.Serializer 10 11 public class Json 12 13 public static string Serializer<T>(T t) 14 15 using (MemoryStream ms = new MemoryStream()) 16 17 DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T)); 18 ser.WriteObject(ms, t); 19 20 string jsonString = Encoding.UTF8.GetString(ms.ToArray()); 21 string p = @"\\/Date\((\d+)\+\d+\)\\/"; 22 MatchEvaluator matchEvaluator = new MatchEvaluator(ConvertJsonDateToDateString); 23 Regex reg = new Regex(p); 24 jsonString = reg.Replace(jsonString, matchEvaluator); 25 //如果是数组 26 27 if (jsonString.StartsWith("[") && jsonString.EndsWith("]")) 28 29 string name = t.GetType().Name; 30 if (t is System.Collections.ArrayList) 31 32 string[] arr = name.Split(new string[] "[]" , StringSplitOptions.None); 33 if (arr.Length > 0) name = arr[0] + "s"; 34 35 else if (t is System.Collections.IList) 36 37 System.Collections.IList list = (System.Collections.IList)t; 38 if (list.Count > 0) name = list[0].GetType().Name + "s"; 39 40 jsonString = "\"" + name + "\":" + jsonString + ""; 41 42 return jsonString; 43 44 45 46 public static T Deserialize<T>(string jsonString) 47 48 //将"yyyy-MM-dd HH:mm:ss"格式的字符串转为"\/Date(1294499956278+0800)\/"格式 49 string p = @"\d4-\d2-\d2\s\d2:\d2:\d2"; 50 MatchEvaluator matchEvaluator = new MatchEvaluator(ConvertDateStringToJsonDate); 51 Regex reg = new Regex(p); 52 jsonString = reg.Replace(jsonString, matchEvaluator); 53 using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString))) 54 55 56 DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T)); 57 T obj = (T)ser.ReadObject(ms); 58 return obj; 59 60 61 62 private static string ConvertJsonDateToDateString(Match m) 63 64 string result = string.Empty; 65 DateTime dt = new DateTime(1970, 1, 1); 66 dt = dt.AddMilliseconds(long.Parse(m.Groups[1].Value)); 67 dt = dt.ToLocalTime(); 68 result = dt.ToString("yyyy-MM-dd HH:mm:ss"); 69 return result; 70 71 72 private static string ConvertDateStringToJsonDate(Match m) 73 74 string result = string.Empty; 75 DateTime dt = DateTime.Parse(m.Groups[0].Value); 76 dt = dt.ToUniversalTime(); 77 TimeSpan ts = dt - DateTime.Parse("1970-01-01"); 78 result = string.Format("\\/Date(0+0800)\\/", ts.TotalMilliseconds); 79 return result; 80 81 82 /// <summary> 83 /// 将对象序列化为JSON格式 84 /// </summary> 85 /// <param name="o">对象</param> 86 /// <returns>json字符串</returns> 87 public static string SerializeObject(object o) 88 89 string json = JsonConvert.SerializeObject(o); 90 return json; 91 92 93 /// <summary> 94 /// 解析JSON字符串生成对象实体 95 /// </summary> 96 /// <typeparam name="T">对象类型</typeparam> 97 /// <param name="json">json字符串(eg."ID":"112","Name":"石子儿")</param> 98 /// <returns>对象实体</returns> 99 public static T DeserializeJsonToObject<T>(string json) where T : class 100 101 JsonSerializer serializer = new JsonSerializer(); 102 StringReader sr = new StringReader(json); 103 object o = serializer.Deserialize(new JsonTextReader(sr), typeof(T)); 104 T t = o as T; 105 return t; 106 107 108 /// <summary> 109 /// 解析JSON数组生成对象实体集合 110 /// </summary> 111 /// <typeparam name="T">对象类型</typeparam> 112 /// <param name="json">json数组字符串(eg.["ID":"112","Name":"石子儿"])</param> 113 /// <returns>对象实体集合</returns> 114 public static List<T> DeserializeJsonToList<T>(string json) where T : class 115 116 JsonSerializer serializer = new JsonSerializer(); 117 StringReader sr = new StringReader(json); 118 object o = serializer.Deserialize(new JsonTextReader(sr), typeof(List<T>)); 119 List<T> list = o as List<T>; 120 return list; 121 122 123 /// <summary> 124 /// 反序列化JSON到给定的匿名对象. 125 /// </summary> 126 /// <typeparam name="T">匿名对象类型</typeparam> 127 /// <param name="json">json字符串</param> 128 /// <param name="anonymousTypeObject">匿名对象</param> 129 /// <returns>匿名对象</returns> 130 public static T DeserializeAnonymousType<T>(string json, T anonymousTypeObject) 131 132 T t = JsonConvert.DeserializeAnonymousType(json, anonymousTypeObject); 133 return t; 134 135 136
以上是关于Json的使用的主要内容,如果未能解决你的问题,请参考以下文章