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.
First, C # processing simple JSON data
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:
public struct Tojsonmy {public string result {get; set;} The name of the property must be the same as the "key" value in the JSON format string. Public string Res_info {get; set;} public string Queryorder_info {get; set;} }
Conversion process:
public static void Jsonmy () { string json = Jsonstr ("D:\\json\\jsonmy1.txt");//jsonstr function reads the text txt of the JSON data JavaScriptSerializer js = new JavaScriptSerializer (); Instantiate a class that can serialize data tojsonmy list = js. Deserialize<tojsonmy> (JSON); Converts the JSON data into an object type and assigns a value to list string result = List.result; string res_info = List.res_info; string queryorder_info = List.res_info;}
Second, C # processing JSON data containing objects
JSON data: Jsonmy2.txt
{"Result": "0", "res_info": "OK", "Queryorder_info": {"Order_num": "5", "OrderDetail": "OK"}}
Construct the object:
public struct tojsonmy2{public string result {get; set;} public string Res_info {get; set;} Public Queryorder_info Queryorder_info;} public struct queryorder_info{public string Order_num {get; set;} public string OrderDetail {get; set;}};
Conversion process
public static void JsonMy2 () { string json = Jsonstr ("D:\\json\\jsonmy2.txt"); JavaScriptSerializer js = new JavaScriptSerializer (); Instantiate a class that can serialize data ToJsonMy2 list = js. Deserialize<tojsonmy2> (JSON); Converts the JSON data into an object type and assigns a value to list string result = List.result; string res_info = List.res_info; string order_num = List.queryorder_info.order_num; string orderdetail = List.queryorder_info.orderdetail;}
C # Processing of JSON data containing objects, arrays
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 struct tojsonmy3{public string result {get; set;} public string Res_info {get; set;} Public Queryorder_info Queryorder_info;} public struct queryorder_info{public string Order_num {get; set;} Public list<orderdetail> orderdetail;//Array processing };p ublic struct orderdetail{public string Cftuin {get; set;} Public string Canceldeadline {get; set;} public string Checkindate {get; set;} public string Checkoutdate {get; set;} public string Cityid {get; set;} public string CurrencyCode {get; set;} public string Hotelid {get; set;} public string Hotelname {get; set;} public string ListID {get; set;} public string Payamt {get; set;} public string PayType {get; set;} public string rommscnt {get; set;} public string Sptransid {get; set;} public string State {get; set;}};
Conversion process:
public static void JsonMy4 () {String json = Jsonstr ("D:\\json\\jsonmy4.txt"); JavaScriptSerializer js = new JavaScriptSerializer (); Instantiate a class that can serialize data ToJsonMy3 list = js. Deserialize<tojsonmy3> (JSON); Converts the JSON data into an object type and assigns a value to list string result = List.result; string res_info = List.res_info; string order_num = List.queryorder_info.order_num; list<orderdetail> OrderDetail = List.queryorder_info.orderdetail; String Cftuin = Orderdetail[0]. Cftuin; String hotelname = Orderdetail[0]. Hotelname; String ListID = Orderdetail[1]. ListID; string state = Orderdetail[2]. State;}