c handles instances of three types of json data

  • 2020-06-15 10:09:24
  • OfStack

In the network, data transmission is often xml or json. Before I worked on a project, other system interfaces were returned in xml format. I just encountered an interface that returned data in json format.
1. C# handles simple json data
json data:

{"result":"0","res_info":"ok","queryorder_info":"info"}

I saved it as es13EN1.txt file in d disk json folder.

Construction object:

 public struct ToJsonMy
 {
    public string result { get; set; }  // The name of the attribute must be associated with json In the format string "key" value 1 The sample.
    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 to read json Text of data txt                
    JavaScriptSerializer js = new JavaScriptSerializer();   // instantiation 1 Three classes that can serialize data
    ToJsonMy list = js.Deserialize<ToJsonMy>(json);    // will json The data is converted to an object type and assigned to list
    string result = list.result;             
    string res_info = list.res_info;
    string queryorder_info = list.res_info;
}

2. C# processes json data containing objects
json data: jsonmy2.ES30en
{"result":"0","res_info":"ok","queryorder_info":{"order_num":"5","orderdetail":"ok"}}

Construction 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();   // instantiation 1 Three classes that can serialize data
    ToJsonMy2 list = js.Deserialize<ToJsonMy2>(json);    // will json The data is converted to an object type and assigned 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;
}

3. C# handles json data containing objects and arrays
json data: jsonmy4.ES45en
{"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 Family chain (Beijing Tiantan South store) (original speed 8 Hotel (Beijing Temple of Heaven South Gate) ","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":" The Cayton Hotel Shanghai ","ListID":"1000000005201308280002999413","PayAmt":"1140","PayType":"0","RommsCnt":"1","SPTransID":"65197226","State":"4"}]}}

Construction 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       
};
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 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();   // instantiation 1 Three classes that can serialize data
    ToJsonMy3 list = js.Deserialize<ToJsonMy3>(json);    // will json The data is converted to an object type and assigned 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;
}

PS: About json operation, here are some more practical json online tools for your reference:

Online JSON code inspection, inspection, beautification, formatting tools:
http://tools.ofstack.com/code/json

JSON online formatting tool:
http://tools.ofstack.com/code/jsonformat

Online XML/JSON interconversion tool:
http://tools.ofstack.com/code/xmljson

json code online formatting/beautification/compression/editing/conversion tool:
http://tools.ofstack.com/code/jsoncodeformat

Online json compression/escape tool:

http://tools.ofstack.com/code/json_yasuo_trans

C language Style /HTML/CSS/json code Formatting tool:
http://tools.ofstack.com/code/ccode_html_css_json


Related articles: