C # JSON data parsing-converting string JSON-formatted data to objects

Source: Internet
Author: User

Data transmission in the network is often XML or JSON, now do a project before the other system interface is returned by the XML format, just encountered a return JSON format data interface, through an example from easy to difficult to summarize the processing process, hoping to help the same as I started not friends.

1.json Simple Object

JSON data:

{"Result": "0", "res_info": "OK", "Queryorder_info": "Info"}I am here in the form of a jsonmy1.txt file saved in the D-Disk JSON folder.  construct the object:
   struct   tojsonmy { public  string  Result {get ; set ; } //  public  string  res_info {get ; set          public  string   Queryorder_info {get ; set    }

Conversion process:

  Public Static voidjsonmy () {stringJSON = JSONSTR ("D:\\json\\jsonmy1.txt");//the JSONSTR function reads the text txt of the JSON dataJavaScriptSerializer js =NewJavaScriptSerializer ();//instantiate a class that can serialize dataTojsonmy list = js. Deserialize<tojsonmy> (JSON);//Convert the JSON data to an object type and assign a value to the list            stringresult = List.result;       
String
Res_info =List.res_info; stringQueryorder_info =List.res_info; }

2.json also contains objects

JSON data: Jsonmy2.txt

{"Result": "0", "res_info": "OK", "Queryorder_info": {"Order_num": "5", "OrderDetail": "OK"}}construct the object:
 Public structToJsonMy2 { Public stringResult {Get;Set; }  Public stringRes_info {Get;Set; }  PublicQueryorder_info Queryorder_info; } Public structQueryorder_info { Public stringOrder_num {Get;Set; }  Public stringOrderDetail {Get;Set; } };

Conversion process:

   Public Static voidJsonMy2 () {stringJSON = JSONSTR ("D:\\json\\jsonmy2.txt"); JavaScriptSerializer JS=NewJavaScriptSerializer ();//instantiate a class that can serialize dataToJsonMy2 list = js. Deserialize<tojsonmy2> (JSON);//Convert the JSON data to an object type and assign a value to the list            stringresult =List.result; stringRes_info =List.res_info; stringOrder_num =List.queryorder_info.order_num; stringOrderDetail =List.queryorder_info.orderdetail; }

3.json contains an array of objects JSON data: jsonmy4.txt

{"Result": "0", "res_info": "OK", "Queryorder_info": {"Order_num": "5", "OrderDetail": [{"Cftuin": "769839263", " Canceldeadline ":" 2013-09-12 23:00:00 "," checkindate ":" 2013-09-12 00:00:00 "," checkoutdate ":" 2013-09-13 00:00:00 "," Cityid ":" 0101 "," CurrencyCode ":" RMB "," Hotelid ":" 00301105 "," hotelname ":" Le Home Chain (Beijing Temple of Heaven South) "(former Super 8 Hotel (Beijing Temple of Heaven South)", "ListID": " 1000000005201308280002999652 "," Payamt ":" 228 "," PayType ":" 0 "," rommscnt ":" 1 "," Sptransid ":" 65202157 "," state ":" 4 "}, {"Cftuin": "248486133", "Canceldeadline": "2013-10-13 23:00:00", "checkindate": "2013-10-13 00:00:00", "CheckOutDate": "2013-10-18 00:00:00", "Cityid": "0201", "CurrencyCode": "RMB", "Hotelid": "10201314", "Hotelname": "Shanghai Caden Hotel", "ListID": " 1000000005201308280002999413 "," Payamt ":" 1140 "," PayType ":" 0 "," rommscnt ":" 1 "," Sptransid ":" 65197226 "," state ":" 4 "} ]}} construct the object:
 Public structToJsonMy3 { Public stringResult {Get;Set; }  Public stringRes_info {Get;Set; }  PublicQueryorder_info Queryorder_info; }   Public structQueryorder_info { Public stringOrder_num {Get;Set; }  PublicList<orderdetail>OrderDetail;//Array processing       
};
Public structOrderDetail { Public stringCftuin {Get;Set; } Public stringCanceldeadline {Get;Set; } Public stringcheckindate {Get;Set; } Public stringcheckoutdate {Get;Set; } Public stringCityid {Get;Set; } Public stringCurrencyCode {Get;Set; } Public stringHotelid {Get;Set; } Public stringHotelname {Get;Set; } Public stringListID {Get;Set; } Public stringPayamt {Get;Set; } Public stringPayType {Get;Set; } Public stringrommscnt {Get;Set; } Public stringSptransid {Get;Set; } Public stringState {Get;Set; } };

Conversion process:

  Public Static voidJsonMy4 () {stringJSON = JSONSTR ("D:\\json\\jsonmy4.txt"); JavaScriptSerializer JS=NewJavaScriptSerializer ();//instantiate a class that can serialize dataToJsonMy3 list = js. Deserialize<tojsonmy3> (JSON);//Convert the JSON data to an object type and assign a value to the list            stringresult =List.result; stringRes_info =List.res_info; stringOrder_num =List.queryorder_info.order_num; List<orderdetail> OrderDetail =List.queryorder_info.orderdetail; stringCftuin = orderdetail[0].            Cftuin; stringHotelname = orderdetail[0].            Hotelname; stringListID = orderdetail[1].            ListID; stringState = orderdetail[2].        State; }

Source: https://www.cnblogs.com/echolxl/p/3594112.html

C # JSON data parsing-converting string JSON-formatted data to objects

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.