Manipulating JSON-formatted files in C # with JavaScriptSerializer and json.net

Source: Internet
Author: User
Tags net serialization

1.json file

2. Write the corresponding class

1         //Discount2          Public classDiscount3         {4              Public stringQty {Get;Set; }5              Public stringPercentage {Get;Set; }6         }7         //Product Information8          Public classProductInfo9         {Ten              Public stringPacking {Get;Set; } One              Public stringQty {Get;Set; } A              PublicList<discount> Discount {Get;Set; } -         } -         //General Information the          Public classRootobject -         { -              Public stringID {Get;Set; } -              Public stringDate {Get;Set; } +              PublicList<productinfo> ProductInfo {Get;Set; } -}
View Code

3.JavaScriptSerializer Operation JSON

1             stringJsontext = File.readalltext ("json file. JSON");2JavaScriptSerializer JSS =NewJavaScriptSerializer ();3Rootobject root = JSS. Deserialize<rootobject>(jsontext);4Console.WriteLine (Root.id +"=========="+root.date);5Console.WriteLine (root.productinfo[0].packing +"======"+ root.productinfo[0]. Qty);6Console.WriteLine (root.productinfo[0]. discount[0]. Qty +"========"+ root.productinfo[0]. discount[0].percentage);7Console.WriteLine (root.productinfo[0]. discount[1]. Qty +"======="+ root.productinfo[0]. discount[1].percentage);8Console.readkey ();
View Code

3.1 Running Results

4.json.net Operation Json

1             stringJsontext = File.readalltext ("json file. JSON");2             //deserializing JSON strings3Rootobject root=jsonconvert.deserializeobject<rootobject>(jsontext);4Console.WriteLine (Root.id +"=========="+root.date);5Console.WriteLine (root.productinfo[0].packing +"======"+ root.productinfo[0]. Qty);6Console.WriteLine (root.productinfo[0]. discount[0]. Qty +"========"+ root.productinfo[0]. discount[0].percentage);7Console.WriteLine (root.productinfo[0]. discount[1]. Qty +"======="+ root.productinfo[0]. discount[1].percentage);8Console.readkey ();
View Code

4.1 Running Results

4.2 Using Json.NET serialization

1Student stu=NewStudent ();2Stu. Id =1;3Stu. Name ="Kaka West";4Stu. 中文版 = -;5Stu. Math = -;6             stringjsonstring=Jsonconvert.serializeobject (stu);7 Console.WriteLine (jsonstring);8Console.readkey ();
View Code

Manipulating JSON-formatted files in C # with JavaScriptSerializer and json.net

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.