程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> c#處置3種json數據的實例

c#處置3種json數據的實例

編輯:C#入門知識

c#處置3種json數據的實例。本站提示廣大學習愛好者:(c#處置3種json數據的實例)文章只能為提供參考,不一定能成為您想要的結果。以下是c#處置3種json數據的實例正文


收集中數據傳輸常常是xml或許json,如今做的一個項目之前調其他體系接口都是前往的xml格局,方才碰到一個前往json格局數據的接口,經由過程例子由易到難總結一下處置進程,願望能幫到和我一樣開端不會的同伙。
1、C#處置簡略json數據
json數據:
{"result":"0","res_info":"ok","queryorder_info":"info"}
我這裡是以jsonmy1.txt文件的情勢保留在d盤json文件夾下。
 
結構對象:

 public struct ToJsonMy
 {
    public string result { get; set; }  //屬性的名字,必需與json格局字符串中的"key"值一樣。
    public string res_info { get; set; }
    public string queryorder_info { get; set; }  
 }
轉換進程:
public static void JsonMy()
{
    string json = Jsonstr("D:\\json\\jsonmy1.txt");//Jsonstr函數讀取json數據的文本txt                
    JavaScriptSerializer js = new JavaScriptSerializer();   //實例化一個可以或許序列化數據的類
    ToJsonMy list = js.Deserialize<ToJsonMy>(json);    //將json數據轉化為對象類型並賦值給list
    string result = list.result;             
    string res_info = list.res_info;
    string queryorder_info = list.res_info;
}
2、C#處置包括對象的json數據
json數據:jsonmy2.txt
{"result":"0","res_info":"ok","queryorder_info":{"order_num":"5","orderdetail":"ok"}}
結構對象:

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; }
};
轉換進程:
public static void JsonMy2()
{
    string json = Jsonstr("D:\\json\\jsonmy2.txt");
    JavaScriptSerializer js = new JavaScriptSerializer();   //實例化一個可以或許序列化數據的類
    ToJsonMy2 list = js.Deserialize<ToJsonMy2>(json);    //將json數據轉化為對象類型並賦值給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#處置包括對象、數組的json數據
json數據: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":"樂家連鎖(北京天壇南門店)(原速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"}]}}
結構對象:
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;//數組處置      
};
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; }
};

轉換進程:
public static void JsonMy4()
{
    string json = Jsonstr("D:\\json\\jsonmy4.txt");
    JavaScriptSerializer js = new JavaScriptSerializer();   //實例化一個可以或許序列化數據的類
    ToJsonMy3 list = js.Deserialize<ToJsonMy3>(json);    //將json數據轉化為對象類型並賦值給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:關於json操作,這裡再為年夜家推舉幾款比擬適用的json在線對象供年夜家參考應用:

在線JSON代碼磨練、磨練、丑化、格局化對象:
http://tools.jb51.net/code/json

JSON在線格局化對象:
http://tools.jb51.net/code/jsonformat

在線XML/JSON相互轉換對象:
http://tools.jb51.net/code/xmljson

json代碼在線格局化/丑化/緊縮/編纂/轉換對象:
http://tools.jb51.net/code/jsoncodeformat

在線json緊縮/本義對象:

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

C說話作風/HTML/CSS/json代碼格局化丑化對象:
http://tools.jb51.net/code/ccode_html_css_json

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved