欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

c#處理3種json數(shù)據(jù)的實(shí)例

 更新時(shí)間:2014年03月12日 15:17:47   投稿:junjie  
這篇文章主要介紹了c#處理包含數(shù)組、對象的復(fù)雜json數(shù)據(jù)的方法,,需要的朋友可以參考下

網(wǎng)絡(luò)中數(shù)據(jù)傳輸經(jīng)常是xml或者json,現(xiàn)在做的一個(gè)項(xiàng)目之前調(diào)其他系統(tǒng)接口都是返回的xml格式,剛剛遇到一個(gè)返回json格式數(shù)據(jù)的接口,通過例子由易到難總結(jié)一下處理過程,希望能幫到和我一樣開始不會(huì)的朋友。
一、C#處理簡單json數(shù)據(jù)
json數(shù)據(jù):

復(fù)制代碼 代碼如下:
{"result":"0","res_info":"ok","queryorder_info":"info"}

我這里是以jsonmy1.txt文件的形式保存在d盤json文件夾下。
 
構(gòu)造對象:
復(fù)制代碼 代碼如下:

 public struct ToJsonMy
 {
    public string result { get; set; }  //屬性的名字,必須與json格式字符串中的"key"值一樣。
    public string res_info { get; set; }
    public string queryorder_info { get; set; }  
 }

轉(zhuǎn)換過程:
復(fù)制代碼 代碼如下:
public static void JsonMy()
{
    string json = Jsonstr("D:\\json\\jsonmy1.txt");//Jsonstr函數(shù)讀取json數(shù)據(jù)的文本txt                
    JavaScriptSerializer js = new JavaScriptSerializer();   //實(shí)例化一個(gè)能夠序列化數(shù)據(jù)的類
    ToJsonMy list = js.Deserialize<ToJsonMy>(json);    //將json數(shù)據(jù)轉(zhuǎn)化為對象類型并賦值給list
    string result = list.result;             
    string res_info = list.res_info;
    string queryorder_info = list.res_info;
}

二、C#處理包含對象的json數(shù)據(jù)
json數(shù)據(jù):jsonmy2.txt
復(fù)制代碼 代碼如下:
{"result":"0","res_info":"ok","queryorder_info":{"order_num":"5","orderdetail":"ok"}}

構(gòu)造對象:
復(fù)制代碼 代碼如下:

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; }
};

轉(zhuǎn)換過程:
復(fù)制代碼 代碼如下:
public static void JsonMy2()
{
    string json = Jsonstr("D:\\json\\jsonmy2.txt");
    JavaScriptSerializer js = new JavaScriptSerializer();   //實(shí)例化一個(gè)能夠序列化數(shù)據(jù)的類
    ToJsonMy2 list = js.Deserialize<ToJsonMy2>(json);    //將json數(shù)據(jù)轉(zhuǎn)化為對象類型并賦值給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#處理包含對象、數(shù)組的json數(shù)據(jù)
json數(shù)據(jù):jsonmy4.txt
復(fù)制代碼 代碼如下:
{"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":"樂家連鎖(北京天壇南門店)(原速8酒店(北京天壇南門店)","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":"上海凱頓酒店","ListID":"1000000005201308280002999413","PayAmt":"1140","PayType":"0","RommsCnt":"1","SPTransID":"65197226","State":"4"}]}}

構(gòu)造對象:
復(fù)制代碼 代碼如下:
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;//數(shù)組處理      
};
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; }
};

轉(zhuǎn)換過程:

復(fù)制代碼 代碼如下:
public static void JsonMy4()
{
    string json = Jsonstr("D:\\json\\jsonmy4.txt");
    JavaScriptSerializer js = new JavaScriptSerializer();   //實(shí)例化一個(gè)能夠序列化數(shù)據(jù)的類
    ToJsonMy3 list = js.Deserialize<ToJsonMy3>(json);    //將json數(shù)據(jù)轉(zhuǎn)化為對象類型并賦值給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:關(guān)于json操作,這里再為大家推薦幾款比較實(shí)用的json在線工具供大家參考使用:

在線JSON代碼檢驗(yàn)、檢驗(yàn)、美化、格式化工具:
http://tools.jb51.net/code/json

JSON在線格式化工具:
http://tools.jb51.net/code/jsonformat

在線XML/JSON互相轉(zhuǎn)換工具:
http://tools.jb51.net/code/xmljson

json代碼在線格式化/美化/壓縮/編輯/轉(zhuǎn)換工具:
http://tools.jb51.net/code/jsoncodeformat

在線json壓縮/轉(zhuǎn)義工具:

http://tools.jb51.net/code/json_yasuo_trans

C語言風(fēng)格/HTML/CSS/json代碼格式化美化工具:
http://tools.jb51.net/code/ccode_html_css_json

相關(guān)文章

最新評論