JsonHelper.class
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 9 /// JSON序列化和反序列化辅助类 10 /// </summary> 11 public class JsonHelper 12 { 13 /// <summary> 14 /// JSON序列化 15 /// </summary> 16 public static string JsonSerializer<T>(T t) 17 { 18 DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T)); 19 MemoryStream ms = new MemoryStream(); 20 ser.WriteObject(ms, t); 21 string jsonString = Encoding.UTF8.GetString(ms.ToArray()); 22 ms.Close(); 23 return jsonString; 24 } 25 26 /// <summary> 27 /// JSON反序列化 28 /// </summary> 29 public static T JsonDeserialize<T>(string jsonString) 30 { 31 DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T)); 32 MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString)); 33 T obj = (T)ser.ReadObject(ms); 34 return obj; 35 } 36 }
感谢:http://www.cnblogs.com/zhaozhan/archive/2011/01/09/1931340.html
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。