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