首页 > 代码库 > JsonHelper.class

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 JsonHelper12 {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 }
View Code

感谢:http://www.cnblogs.com/zhaozhan/archive/2011/01/09/1931340.html

JsonHelper.class