C # example of processing three types of json data,
Address: http://www.jb51.net/article/48027.htm
This article uses C # parsing JSON method (built-in method) http://www.cnblogs.com/zhaofuchun/p/4434681.html
I. C # Processing simple json data
Json data:
Copy the Code as follows:
{"result":"0","res_info":"ok","queryorder_info":"info"}
I am saving it in the json folder of drive D as a jsonmy1.txt file.
Constructor:
Copy the Code as follows:
Public struct ToJsonMy {public string result {get; set;} // attribute name, which 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:
Copy the Code as follows:
Public static void JsonMy () {string json = Jsonstr ("D :\\ json \ jsonmy1.txt "); // The Jsonstr function reads the json Data Text txt JavaScriptSerializer js = new JavaScriptSerializer (); // instantiate a class ToJsonMy list = js that can serialize data. deserialize <ToJsonMy> (json); // convert json data to the object type and assign it to list string result = list. result; string res_info = list. res_info; string queryorder_info = list. res_info ;}
II. C # process json data containing objects
Json data: jsonmy2.txt
Copy the Code as follows:
{"result":"0","res_info":"ok","queryorder_info":{"order_num":"5","orderdetail":"ok"}}
Constructor:
Copy the Code as follows:
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:
Copy the Code as follows:
Public static void JsonMy2 () {string json = Jsonstr ("D :\\ json \ jsonmy2.txt"); JavaScriptSerializer js = new JavaScriptSerializer (); // instantiate a class ToJsonMy2 list = js. deserialize <ToJsonMy2> (json); // convert json data to the object type and assign it 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 ;}
III. C # process json data containing objects and Arrays
Json data: jsonmy4.txt
Copy the Code as follows:
{"Result": "0", "res_info": "OK", "queryorder_info": {"order_num": "5", "orderdetail": [{"CFTUin ": "769839263", "CancelDeadline": "23:00:00", "CheckInDate": "00:00:00", "CheckOutDate": "00:00:00", "CityID": "0101 ", "CurrencyCode": "RMB", "hyperlink ID": "00301105", "hyperlink name": "lejia chain store (Beijing Tiantan South store) (former Super 8 Hotel (Beijing Tiantan South Gate store), "ListID": "1000000005201308280002999652", "PayAmt": "228", "PayType": "0", "RommsCnt": "1", "SPTransID ": "65202157", "State": "4" },{ "CFTUin": "248486133", "CancelDeadline": "23:00:00", "CheckInDate": "00:00:00 ", "CheckOutDate": "00:00:00", "CityID": "0201", "CurrencyCode": "RMB", "Region ID": "10201314", "region name ": "Shanghai keton liquor store", "ListID": "1000000005201308280002999413", "PayAmt": "1140", "PayType": "0", "RommsCnt": "1 ", "SPTransID": "65197226", "State": "4"}]}
Constructor:
Copy the Code as follows:
Public struct ToJsonMy3 {public string result {get; set;} public string res_info {get; set;} public queryorder_info;} public struct queryorder_info {public string order_num {get; set ;} public List <orderdetail> orderdetail; // Array Processing}; public 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 consumer ID {get; set;} public string consumer name {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:
Copy the Code as follows:
Public static void JsonMy4 () {string json = Jsonstr ("D :\\ json \ jsonmy4.txt"); JavaScriptSerializer js = new JavaScriptSerializer (); // instantiate a class ToJsonMy3 list = js. deserialize <ToJsonMy3> (json); // convert json data to the object type and assign it 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 detail name = orderdetail [0]. pipeline name; string ListID = orderdetail [1]. listID; string State = orderdetail [2]. state ;}