json序列化和反序列化幫助類:using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Runtime.Serialization;using System.Runt...
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的序列化和反序列化。