标签:
/// <summary> /// 对象转换成json /// </summary> /// <typeparam name="T"></typeparam> /// <param name="jsonObject">需要格式化的对象</param> /// <returns>Json字符串</returns> public static string DataContractJsonSerialize<T>(T jsonObject) { DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T)); string json = null; using (MemoryStream ms = new MemoryStream()) //定义一个stream用来存发序列化之后的内容 { serializer.WriteObject(ms, jsonObject); json = Encoding.UTF8.GetString(ms.GetBuffer()); //将stream读取成一个字符串形式的数据,并且返回 ms.Close(); } return json; } /// <summary> /// json字符串转换成对象 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="json">要转换成对象的json字符串</param> /// <returns></returns> public static T DataContractJsonDeserialize<T>(string json) { DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T)); T obj = default(T); using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(json))) { obj = (T)serializer.ReadObject(ms); ms.Close(); } return obj; }
标签:
原文地址:http://www.cnblogs.com/trvatwang/p/4636223.html