Cs. Json; NET 4.0 var data = new Jsonobject (); Data. ADD ("cart_id", 123456); Data. ADD ("Item_count", 8); var items = new Jsonarray (); for (int i = 0; i < 8; i++) {var node = new CS. Json.jsonobject (); Node. ADD ("Item_name", "Project Name"); Node. ADD ("Item_price", "price"); Items. ADD (node); } data. ADD ("Items", items); Response.Write (Jsonconvert.serializeobject (data)); Using Newtonsoft.json; NET 2.0 StringBuilder sb = new StringBuilder (); StringWriter SW = new StringWriter (SB); using (jsonwriter jsonwriter = new JsonTextWriter (sw)) {Jsonserializer ser = new Jsonserializer (); jsonwriter.formatting = formatting.indented; Jsonwriter.writestartobject (); Jsonwriter.writepropertyname ("CPU"); Jsonwriter.writevalue ("Intel"); Jsonwriter.writepropertyname ("PSU"); Jsonwriter.writevalue ("500W"); Jsonwriter.writepropertyname ("Drives"); Jsonwriter.writestartarray (); for (int i = 1; I <= 8; i++) {jsonwriter.writestartobject (); Jsonwriter.writepropertyname ("num"); Ser. Serialize (Jsonwriter,i.tostring ()); Jsonwriter.writeendobject (); } jsonwriter.writeendarray (); Jsonwriter.writestartarray (); Jsonwriter.writevalue ("DVD read/writer"); Jsonwriter.writecomment ("(broken)"); Jsonwriter.writevalue ("gigabyte hard Drive"); Jsonwriter.writevalue ("Gigabype hard Drive"); Jsonwriter.writeendarray (); Jsonwriter.writeendobject (); } Response.Write (sb.) ToString ()); Using Litjson; NET 2.0 Serialization of demo1 jsondata data = new Jsondata (); data["name"] = "Peiandsky"; Data["age"] = 28; data["sex"] = "male"; var items = new Jsondata (); for (int i = 0; i < 8; i++) {var node = new Jsondata (); node["Item_num"] = i.ToString (); Items. ADD (node); } data["items"] =items; String json1 = data. ToJson (); Response.Write (json1+ Environment.NewLine); Serialization of Demo2 jsonwriter writer2 = new Jsonwriter (); Writer2. Writeobjectstart (); Writer2. Writepropertyname ("book"); Writer2. Writeobjectstart (); Writer2. Writepropertyname ("title"); Writer2. Write ("Android game!"); Writer2. Writepropertyname ("author"); Writer2. Write ("Pei"); Writer2. Writepropertyname ("Bookdetail"); Writer2. WriteobjecTstart (); Writer2. Writepropertyname ("pages"); Writer2. Write (429); Writer2. Writepropertyname ("about"); Writer2. Write (NULL); Writer2. Writeobjectend (); Writer2. Writeobjectend (); Writer2. Writepropertyname ("items"); Writer2. Writearraystart (); for (int i = 1; I <= 8; i++) {writer2. Writeobjectstart (); Writer2. Writepropertyname ("Item_num"); Writer2. Write (i); Writer2. Writeobjectend (); } writer2. Writearrayend (); Writer2. Writeobjectend (); Response.Write (Writer2. ToString () + Environment.NewLine); Deserialization demo Jsondata pa = jsonmapper.toobject (@ "{" "Items" ": [{" "num" ":" "001" "},{" "num" ":" "002" "}]}"); for (int i = 0; I < pa["items"]. Count; i++) {Response.Write (pa["Items"][i]["num"] + Environment.NewLine); }
C # JSON usage set