C # Implementation of JSON serialization and deserialization code instances

Source: Internet
Author: User
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>///////</summary> public string name {get {return Name;}    set {name = value;} }}//Method two: Introduce System.Runtime.Serialization.Json namespaces using the DataContractJsonSerializer class to implement serialization//use of Ignoredatamember:  Specifies that the member is not part of a 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["Conns TR "].         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)))//{//Data       Contractjsonserializer serializer = new DataContractJsonSerializer (typeof (People)); Person1 _people = (Person1) serializer.       ReadObject (MS);    }} protected void button1_click (object sender, EventArgs e) {Response.Write (CONSTR); }    }
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.