C # JSON usage set

Source: Internet
Author: User

            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

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.