asp.net json 与xml 的基础事例
1 //json序列化和反序列化 using System.Runtime.Serialization.Json; 2 public static string JsonSerializer<T>(T t) 3 { 4 DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T)); 5 MemoryStream ms = new MemoryStream(); 6 ser.WriteObject(ms, t); 7 string jsonString = Encoding.UTF8.GetString(ms.ToArray()); 8 ms.Close(); 9 //替换Json的Date字符串 10 string p = @"\\/Date\((\d+)\+\d+\)\\/"; 11 MatchEvaluator matchEvaluator = new MatchEvaluator(ConvertJsonDateToDateString); 12 Regex reg = new Regex(p); 13 jsonString = reg.Replace(jsonString,matchEvaluator); 14 return jsonString; 15 } 16 17 //反序列化 18 public static T JsonDeserialize<T>(string jsonString) 19 { 20 //将"yyyy-MM-dd HH:mm:ss"格式的字符串转为"\/Date(1294499956278+0800)\/"格式 string p = @"\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}"; 21 string p = @"\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}"; 22 MatchEvaluator matchEvaluator = new MatchEvaluator(ConvertDateStringToJsonDate); 23 Regex reg = new Regex(p); 24 jsonString = reg.Replace(jsonString, matchEvaluator); 25 DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T)); 26 MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString)); 27 T obj = (T)ser.ReadObject(ms); 28 return obj; 29 } 30 31 32 33 /// <summary> 34 /// /// 将Json序列化的时间由/Date(1294499956278+0800)转为字符串 35 /// /// </summary> 36 /// 37 38 private static string ConvertJsonDateToDateString(Match m) 39 { 40 string result = string.Empty; 41 DateTime dt = new DateTime(1970,1,1); 42 dt = dt.AddMilliseconds(long.Parse(m.Groups[1].Value)); 43 dt = dt.ToLocalTime(); 44 result = dt.ToString("yyyy-MM-dd HH:mm:ss"); 45 return result; 46 } 47 /// <summary> 48 /// 49 /// 将时间字符串转为Json时间 50 /// /// </summary> 51 /// 52 53 private static string ConvertDateStringToJsonDate(Match m) 54 { 55 string result = string.Empty; 56 DateTime dt = DateTime.Parse(m.Groups[0].Value); 57 dt = dt.ToUniversalTime(); 58 TimeSpan ts = dt - DateTime.Parse("1970-01-01"); 59 result = string.Format("\\/Date({0}+0800)\\/",ts.TotalMilliseconds); 60 return result; 61 } 62 63 public class Person 64 { 65 public string Name { get; set; } 66 public int Age { get; set; } 67 public DateTime LastLoginTime { get; set; } 68 } 69 70 71 //序列化 72 Person p = new Person(); 73 p.Name = "张三"; 74 p.Age = 28; 75 p.LastLoginTime = DateTime.Now; 76 string jsonString = JsonSerializer<Person>(p); 77 Response.Write(jsonString); 78 // 反序列化 79 string json = "{\"Age\":28,\"LastLoginTime\":\"2011-01-09 00:30:00\",\"Name\":\"张三\"}"; 80 Person p = JsonDeserialize<Person>(json); 81 Response.Write("姓名:" + p.Name + "</br>"); 82 Response.Write("年龄:" + p.Age+"</br>"); 83 Response.Write("时间:" + p.LastLoginTime); 84 85 List<Person>list=new List <Person>{ 86 new Person{Name="张三",Age=24}, 87 new Person{Name="小白",Age=5} 88 }; 89 90 string jsonString = JsonSerializer<List< Person>>(list); 91 Response.Write(jsonString); 92 93 Dictionary<string,string>Dic=new Dictionary<string,string>(); 94 Dic.Add("Name", "24"); 95 Dic.Add("Age","28"); 96 string jsonString=JsonSerializer<Dictionary<string,string>>(Dic); 97 Response.Write(jsonString);
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。