C # implements JSON serialization and deserialization of instance code

Source: Internet
Author: User
When I do HTTP communication between ASP and unity, when the unity client sends out the form request, I want to return the data he wants to request in JSON format to the client, so that the client can parse it. The server side of this piece involves the serialization and deserialization of JSON.

Both methods have examples, the first is to store objects with a generic collection, then serialize the collection, and the second is a directly serialized object

Using system;using system.collections.generic;using system.web.script.serialization;using System.Configuration; Using system.runtime.serialization.json;using system.runtime.serialization;using system.io;using System.Text; namespace webapplication1{//method One: Introduce a System.Web.Script.Serialization namespace using the JavaScriptSerializer class for simple serialization [        Serializable] public class Person {private int id;             <summary>///ID//</summary> public int ID {get {return id;}        set {id = value;}        } private string name; <summary>//name////</summary> public string name {get {return n Ame        } set {name = value;} }}//Method Two: Introduce the System.Runtime.Serialization.Json namespace using the DataContractJsonSerializer class to implement serialization//You can use Ignoredatamember: Specify the    Member is not part of the data contract and is not serialized, DataMember: Defining a serialization attribute parameter, using the DataMember property tag field must use the DataContract tag class otherwise the DataMember tag does not work. [DataconTract] public class Person1 {[ignoredatamember] public int Id {get; set;}        [DataMember (name = "name")] public string Name {get; set;}    [DataMember (Name = "sex")] public string sex {get; set;} } public partial class _default:system.web.ui.page {string constr = Configurationmanager.connectionstrings ["ConnStr"].        ConnectionString;            protected void Page_Load (object sender, EventArgs e) {person P1 = new person (); P1.            Id = 1; P1.            Name = "Dxw";            person P2 = new person (); P2.            Id = 2; P2.            Name = "WN";            list<person> Listperson = new list<person> (); Listperson.            Add (p1); Listperson.            ADD (p2);            JavaScriptSerializer js = new JavaScriptSerializer (); JSON serialization of string s = js.            Serialize (Listperson);             Response.Write (s);      Method two Person1 P11 = new Person1 ();      P11.            Id = 1; P11.            Name = "Hello"; P11.            Sex = "male"; DataContractJsonSerializer json = new DataContractJsonSerializer (P11.            GetType ());            String Szjson = ""; Serialization using (MemoryStream stream = new MemoryStream ()) {json.                WriteObject (stream, p11); Szjson = Encoding.UTF8.GetString (stream.                ToArray ());            Response.Write (Szjson);             }//Deserialization//using (MemoryStream ms = new MemoryStream (Encoding.UTF8.GetBytes (Szjson)))//{            DataContractJsonSerializer serializer = new DataContractJsonSerializer (typeof (People)); Person1 _people = (Person1) serializer.            ReadObject (MS); }} protected void button1_click (object sender, EventArgs e) {Response.Write (Co        NSTR); }    }
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.