json序列化和反序列化帮助类:
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Runtime.Serialization;using System.Runtime.Serialization.Json;using System.IO;using System.Text.RegularExPRessions;using System.Web.Script.Serialization;namespace HelpClass.TypeHelp{ /// <summary> /// 在VS中需要添加引用System.Web.Script.Serialization的时候,请先引用System.Web.Extensions /// </summary> public class JsonHelp { /// <summary> /// json序列化(非二进制方式) /// </summary> /// <typeparam name="T"></typeparam> /// <param name="t"></param> /// <returns></returns> public static string JsonSerializer<T>(T t) { javaScriptSerializer jsonSerialize = new JavascriptSerializer(); return jsonSerialize.Serialize(t); } /// <summary> /// json反序列化(非二进制方式) /// </summary> /// <typeparam name="T"></typeparam> /// <param name="jsonString"></param> /// <returns></returns> public static T JsonDeserialize<T>(string jsonString) { JavaScriptSerializer jsonSerialize = new JavaScriptSerializer(); return (T)jsonSerialize.Deserialize<T>(jsonString); } /// <summary> /// JSON序列化(二进制方式,实体类使用[Serializable]) /// </summary> /// <typeparam name="T"></typeparam> /// <param name="t"></param> /// <returns></returns> public static string JsonSerializerIO<T>(T t) { DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T)); using (MemoryStream ms = new MemoryStream()) { ser.WriteObject(ms, t); string jsonString = Encoding.UTF8.GetString(ms.ToArray()); ms.Close(); return jsonString; } } /// <summary> /// JSON反序列化(二进制方法,实体类使用[Serializable]) /// </summary> /// <typeparam name="T"></typeparam> /// <param name="jsonString"></param> /// <returns></returns> public static T JsonDeserializeIO<T>(string jsonString) { DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T)); using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString))) { T obj = (T)ser.ReadObject(ms); return obj; } } }}
上面这个类使用了两中json序列化形式。
1、 使用System.Web.Script.Serialization进行序列化和反序列化,不过在VS中需要添加引用System.Web.Script.Serialization的时候,请先引用System.Web.Extensions。
使用方法:
(1)需要序列化和反序列化的类
public class ddd{ public string d1 { get; set; } public string d2 { get; set; } public string d3 { get; set; }}
(2)序列化和反序列化
序列化单个类为json:
ddd a = new ddd { d1 = "1", d2 = "2", d3 = "3" };string stra = HelpClass.TypeHelp.JsonHelp.JsonSerializer<ddd>(a);ddd dd2 = HelpClass.TypeHelp.JsonHelp.JsonDeserialize<ddd>(stra);序列化类序列为json数组:ddd a = new ddd { d1 = "1", d2 = "2", d3 = "3" };ddd b = new ddd { d1 = "11", d2 = "12", d3 = "13" };ddd c = new ddd { d1 = "21", d2 = "22", d3 = "23" };List<ddd> abc = new List<ddd>();abc.Add(a);abc.Add(b);abc.Add(c);string strabc = HelpClass.TypeHelp.JsonHelp.JsonSerializer<List<ddd>>(abc);List<ddd> ddabc =HelpClass.TypeHelp.JsonHelp.JsonDeserialize<List<ddd>>(strabc);
2、 使用System.Runtime.Serialization.Json命名空间下的DataContractJsonSerializer类进行json的序列化和反序列化,该方法使用的二进制的方式来序列化和反序列化,使用该类方法时需要在对应的实体类中有相应的标识(如:[DataContract] [DataMember(Name = "")])在下面的调用时有相关说明。
使用方法:
(1)需要序列化和反序列化的类
[DataContract]public class ddd{ [DataMember(Name = "d1")] public string d1 { get; set; } [DataMember(Name = "d2")] public string d2 { get; set; } [DataMember(Name = "d3")] public string d3 { get; set; } }
(2)序列化和反序列化
序列化单个类为json:
ddd a = new ddd { d1 = "1", d2 = "2", d3 = "3" };string stra = HelpClass.TypeHelp.JsonHelp.JsonSerializer<ddd>(a);ddd dd2 = HelpClass.TypeHelp.JsonHelp.JsonDeserialize<ddd>(stra);序列化类序列为json数组:ddd a = new ddd { d1 = "1", d2 = "2", d3 = "3" };ddd b = new ddd { d1 = "11", d2 = "12", d3 = "13" };ddd c = new ddd { d1 = "21", d2 = "22", d3 = "23" };List<ddd> abc = new List<ddd>();abc.Add(a);abc.Add(b);abc.Add(c);string strabc = HelpClass.TypeHelp.JsonHelp.JsonSerializer<List<ddd>>(abc);List<ddd> ddabc =HelpClass.TypeHelp.JsonHelp.JsonDeserialize<List<ddd>>(strabc);
看完了上面的这些或许你已经对JSON的序列化和反序列化有了一定的了解,今天就写到这里,下次就总结一下xml的序列化和反序列化。
新闻热点
疑难解答