JSON serialization and deserialization in C #

Source: Internet
Author: User

JSON serialization and deserialization helper 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  >///When you need to add a reference System.Web.Script.Serialization in VS, please refer to System.Web.Extensions//</summary> public class first Jsonhelp {//<summary>///JSON serialization (not binary)///</summary>//<typeparam        Name= "T" ></typeparam>//<param name= "T" ></param>//<returns></returns> public static string jsonserializer<t> (T-t) {JavaScriptSerializer jsonserialize = new Java            Scriptserializer ();        return Jsonserialize.serialize (t); }///<summary>///JSON deserialization (not binary)///</summary>//<typeparam name= "T" >   </typeparam>     <param name= "jsonstring" ></param>///<returns></returns> public static T J Sondeserialize<t> (String jsonstring) {JavaScriptSerializer jsonserialize = new Javascriptserializ            ER ();        Return (T) jsonserialize.deserialize<t> (jsonstring); }///<summary>///JSON serialization (binary mode, entity class using [Serializable])////</summary>//<type param name= "T" ></typeparam>//<param name= "T" ></param>//<returns></return s> 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 using [Serializable])///</summary>//&lt ; Typeparam name= "T" ></typeparam>//<param name= "jsonstring" ></param>//<returns& gt;</returns> public static T jsondeserializeio<t> (String jsonstring) {DATACONTRACTJ            Sonserializer 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 for serialization and deserialization, but when you need to add a reference System.Web.Script.Serialization in VS, refer to System.Web.Extensions first.

How to use:

(1) Classes that need 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);dd D dd2 = helpclass.typehelp.jsonhelp.jsondeserialize<ddd> (stra); Serialization class sequence is a JSON array: ddd A = new DDD {d1 = "1", D2 = "2", D 3 = "3"};ddd B = new DDD {d1 = "one", D2 = "", D3 = "23"};ddd C = new DDD {d1 = "+", D2 = "$", D3 = ""}; 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. Using the DataContractJsonSerializer class under the System.Runtime.Serialization.Json namespace for JSON serialization and deserialization, the method uses the binary way to serialize and deserialize, and the class method needs to be used in the corresponding entity The corresponding identity in the class (for example: [DataContract] [DataMember (Name = "")]) is described in the following call.

How to use:

(1) Classes that need 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);dd D dd2 = helpclass.typehelp.jsonhelp.jsondeserialize<ddd> (stra); Serialization class sequence is a JSON array: ddd A = new DDD {d1 = "1", D2 = "2", D 3 = "3"};ddd B = new DDD {d1 = "one", D2 = "", D3 = "23"};ddd C = new DDD {d1 = "+", D2 = "$", D3 = ""}; 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, perhaps you have a certain understanding of JSON serialization and deserialization, write it here today, next time to summarize the serialization and deserialization of XML.

JSON serialization and deserialization in C #

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.