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