1. 程式人生 > >C#處理JSON 資料

C#處理JSON 資料

網路中資料傳輸經常是xml或者json,現在做的一個專案之前調其他系統介面都是返回的xml格式,剛剛遇到一個返回json格式資料的介面,通過例子由易到難總結一下處理過程,希望能幫到和我一樣開始不會的朋友。
一、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;
}

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

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

在專案中也遇到了使用JSON的情況,就把轉化過程寫一下。

1.從資料庫獲取的json資料為:

[{"ACTIONVALUE":"-1","CONTENT":"","DEVID":"30000449636001","DEVNAME":"#1空調","DEVTYPE":"46","EVENTTYPE":"","GRIDCODE":"","ISAUTO":"false","MEASURENAME":"通用.低風","MEASURETAG":"GEN.CtrlLowWind","STREAMTYPE":null,"TIMESPAN":""},{"ACTIONVALUE":"0","CONTENT":"","DEVID":"30000449660712","DEVNAME":"#1風機","DEVTYPE":"56","EVENTTYPE":"","GRIDCODE":"","ISAUTO":"false","MEASURENAME":"風機開關","MEASURETAG":"GEN.UnibitCtrl","STREAMTYPE":null,"TIMESPAN":""},{"ACTIONVALUE":"1","CONTENT":"","DEVID":"30000449660710","DEVNAME":"#1警笛","DEVTYPE":"57","EVENTTYPE":"","GRIDCODE":"","ISAUTO":"false","MEASURENAME":"警笛開關","MEASURETAG":"GEN.UnibitCtrl","STREAMTYPE":null,"TIMESPAN":""},{"ACTIONVALUE":"1","CONTENT":"","DEVID":"30000449660708","DEVNAME":"#1燈光","DEVTYPE":"45","EVENTTYPE":"","GRIDCODE":"","ISAUTO":"true","MEASURENAME":"燈光開光","MEASURETAG":"GEN.UnibitCtrl","STREAMTYPE":null,"TIMESPAN":""},{"ACTIONVALUE":"66","CONTENT":"","DEVID":"30000449667767","DEVNAME":"hik1","DEVTYPE":"49","EVENTTYPE":"","GRIDCODE":"110100000103010001","ISAUTO":"true","MEASURENAME":"","MEASURETAG":"GEN.PreSet","STREAMTYPE":"150","TIMESPAN":""},{"ACTIONVALUE":"-1","CONTENT":"","DEVID":"30000449667767","DEVNAME":"hik1","DEVTYPE":"49","EVENTTYPE":"","GRIDCODE":"110100000103010001","ISAUTO":"true","MEASURENAME":"","MEASURETAG":"GEN.VideoRecord","STREAMTYPE":"150","TIMESPAN":""}]

2.構造實體物件:

 public class ActionParam
    {
        //[{"ACTIONVALUE":"1","CONTENT":"","DEVID":"30000449660708","DEVNAME":"#1燈光","DEVTYPE":"45","EVENTTYPE":"","GRIDCODE":"","ISAUTO":"true","MEASURENAME":"燈光開光","MEASURETAG":"GEN.UnibitCtrl","STREAMTYPE":null,"TIMESPAN":""}]
        public int ACTIONVALUE { get; set; }
        public string CONTENT { get; set; }
        public string DEVID { get; set; }
        public string DEVNAME { get; set; }
        public string DEVTYPE { get; set; }
        public string EVENTTYPE { get; set; }
        public string GRIDCODE { get; set; }
        public bool ISAUTO { get; set; }
        public string MEASURENAME { get; set; }
        public string MEASURETAG { get; set; }
        public string STREAMTYPE { get; set; }
        public string TIMESPAN { get; set; }

    }

3.轉化為C#中宣告的資料格式:

JavaScriptSerializer js = new JavaScriptSerializer();   //例項化一個能夠序列化資料的類
 List<ActionParam> listDetail = js.Deserialize<List<ActionParam>>(actionParam);

以為json字串是陣列格式,所以js.Deserialize<T>()泛型中使用List<ActionParam>.