public class JsonSerializer { /// <summary> /// json序列化 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="t"></param> /// <returns></returns> public static string JsonStringSerializer<T>(T t) { DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T)); using (MemoryStream ms = new MemoryStream()) { ser.WriteObject(ms, t); string json = Encoding.UTF8.GetString(ms.ToArray()); ms.Close(); return json; } } /// <summary> /// json還原序列化 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="json"></param> /// <returns></returns> public static T DeJsonSerializer<T>(string json) { DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T)); using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(json))) { object obj=ser.ReadObject(ms); ms.Close(); if (obj == null) { throw new NotImplementedException("序列化實體為NULL,json:" + json); } return (T)obj; } } }
Josn序列化與還原序列化demo
C# Json 序列化與還原序列化二
以上就是C# Json 序列化與還原序列化一的內容,更多相關內容請關注topic.alibabacloud.com(www.php.cn)!