This article mainly introduced the C # Implementation of JSON serialization and deserialization of code examples, this article explained two implementation methods, and directly give code examples, the need for friends can refer to the
?
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26-27--28 29---30 31--32 33 34 35 36 37 38-39 40 41 42 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 5, 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 11 9 120 121 122 123 124 |
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: Introduction of System.Web.Script.Serialization namespace use The JavaScriptSerializer class implements a simple serialization [Serializable] public class Person { private int id;///<summary>///ID///& lt;/summary> public int Id {get {return id;} set {id = value;}} private string name; <summary>///name///</summary> public string name {get {return Name =} set {name = value;}}} /Method Two: Introducing the System.Runtime.Serialization.Json namespace using the DataContractJsonSerializer class to implement serialization//can use Ignoredatamember: Specifies that the member is not part of a data contract and is not serialized, DataMember: Defines a serialization attribute argument, and 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.P Age {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 serializes 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))) //{ //D Atacontractjsonserializer serializer = new DataContractJsonSerializer (typeof (People)); //Person1 _people = (Person1) serializer. ReadObject (MS); //} protected void Button1_Click (object sender, EventArgs e) {Response.Write (constr);} &NB Sp } |