JSON serialization and deserialization in C,
Json serialization and deserialization help classes:
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> // you need to add reference System in. web. script. during Serialization, reference System first. web. extensions // </summary> public class JsonHelp {// <summary> // json serialization (non-binary) /// </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 deserialization (non-binary) /// </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 serialization (in binary mode, the object class uses [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 deserialization (Binary method, entity class uses [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 ;}}}}
The above class uses two json serialization forms.
1. Use System. web. script. serialization is used for Serialization and deserialization. However, in VS, you must add a reference System. web. script. during Serialization, reference System first. web. extensions.
Usage:
(1) classes to be serialized and deserialized
public class ddd{ public string d1 { get; set; } public string d2 { get; set; } public string d3 { get; set; }}
(2) serialization and deserialization
Serialize a single class as 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); the serialization class sequence is a json array: 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. Use System. runtime. serialization. the DataContractJsonSerializer class in the Json namespace performs json serialization and deserialization. This method uses the binary method for serialization and deserialization, when using this method, you must have the corresponding identifier (for example, [DataContract] [DataMember (Name = "")]) in the corresponding object class.
Usage:
(1) classes to be serialized and deserialized
[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) serialization and deserialization
Serialize a single class as 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); the serialization class sequence is a json array: 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 );
After reading the above, you may have some knowledge about JSON serialization and deserialization. Today, I will write it here. Next time I will summarize the XML serialization and deserialization.