JSON serialization and deserialization in C #

Source: Internet
Author: User

I haven't written a technical blog in the blog garden for a while, but I still have a blog in the blog garden every day. There is no end to learning ...... Recently, I have written some programs that call others' interfaces and used a lot of JSON, XML serialization and deserialization. Today I will summarize the implementation of json serialization and deserialization, I have a lot of comments from Boyou in wangyuan. Json serialization and deserialization help class: copy the code 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 // </summa Ry> 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> pub Lic 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> /// <par Am 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 ;}}} copies the code above which the 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) public class ddd {public string d1 {get; set;} public string d2 {get; set ;} public string d3 {get; set ;}} copy code (2) serialize and deserialize a single class as json: Copy code 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); copy code 2. Use System. runtime. serialization. dataContractJson in the Json namespace The Serializer class is used for json serialization and deserialization. This method uses the binary method for serialization and deserialization. When using this method, the corresponding entity class must have a corresponding identifier (for example: [DataContract] [DataMember (Name =. Usage: (1) code for copying classes that require serialization and deserialization [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 ;}} copy code (2) serialize and deserialize a single class is json: Copy code ddd a = new ddd {d1 = "1", d2 = "2 ", d3 = "3"}; string stra = HelpClass. typeHelp. jsonHelp. jsonSerializer <ddd> (a); ddd dd2 = HelpClass. typ EHelp. 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. jsonDe Serialize <List <ddd> (strabc); after the code is copied, you may have read about JSON serialization and deserialization. I will write it here today, next, we will summarize the serialization and deserialization of XML.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.