C # implements serialization and deserialization of JSON

Source: Internet
Author: User

usingSystem;usingSystem.Collections.Generic;usingSystem.Web.Script.Serialization;usingSystem.Configuration;usingSystem.Runtime.Serialization.Json;usingSystem.Runtime.Serialization;usingSystem.IO;usingSystem.Text;namespacewebapplication1{//method One: Introduce a System.Web.Script.Serialization namespace using the JavaScriptSerializer class for simple serialization[Serializable] Public classPerson {Private intID; /// <summary>        ///ID/// </summary>         Public intId {Get{returnID;} Set{id =value;} }        Private stringname; /// <summary>        ///name/// </summary>         Public stringName {Get{returnname;} Set{name =value;} }    }    //method Two: Introduce System.Runtime.Serialization.Json namespaces using the DataContractJsonSerializer class for serialization//You can use Ignoredatamember: to specify that 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 classPerson1 {[Ignoredatamember] Public intId {Get;Set; } [DataMember (Name="name")]         Public stringName {Get;Set; } [DataMember (Name="Sex")]         Public stringSex {Get;Set; } }     Public Partial class_default:system.web.ui.page {stringConstr = configurationmanager.connectionstrings["ConnStr"].                ConnectionString; protected voidPage_Load (Objectsender, EventArgs e) {person P1=NewPerson (); P1. Id=1; P1. Name="Dxw"; Person P2=NewPerson (); P2. Id=2; P2. Name="WN"; List<Person> Listperson =NewList<person>(); Listperson.            Add (p1); Listperson.            ADD (p2); JavaScriptSerializer JS=NewJavaScriptSerializer (); //JSON serialization            strings =JS.            Serialize (Listperson);            Response.Write (s); //Method TwoPerson1 P11 =NewPerson1 (); P11. Id=1; P11. Name="Hello"; P11. Sex="male"; DataContractJsonSerializer JSON=NewDataContractJsonSerializer (P11.            GetType ()); stringSzjson =""; //Serialization of            using(MemoryStream stream =NewMemoryStream ()) {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 voidButton1_Click (Objectsender, EventArgs e)        {Response.Write (CONSTR); }    }

C # implements serialization and deserialization of JSON

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.