C#如何解析JSON數據(反序列化對象)
第一章:C#如何拿到從http上返回JSON數據?
第二章:C#如何解析JSON數據?(反序列化對象)
第三章:C#如何生成JSON字符串?(序列化對象)
第四章:C#如何生成JSON字符串提交給接口(服務器)
在上一篇文章中,我們講解了如何通過API接口獲取返回的JSON字符串,那么,這篇文章我們來講解拿到了返回的JSON字符串后,我們要如何取到里面我們需要的數據呢?這操作叫JSON的反序列化操作。接下里我們將一一解釋。
先看效果:這個大家最喜歡。
我們先看一下上一篇文章中返回的字符串。
{"message":"ok","nu":"367847964498","ischeck":"1","condition":"F00","com":"shunfeng","status":"200","state":"3","data":[{"time":"2017-09-21 09:33:09","ftime":"2017-09-21 09:33:09","context":"已簽收,感謝使用順豐,期待再次為您服務","location":""},{"time":"2017-09-21 09:09:48","ftime":"2017-09-21 09:09:48","context":"快件交給鞏向濤,正在派送途中(聯系電話:18806439871)","location":""},{"time":"2017-09-21 07:02:41","ftime":"2017-09-21 07:02:41","context":"快件到達 【淄博市桓台田庄速運營業點 】","location":""},{"time":"2017-09-20 15:32:00","ftime":"2017-09-20 15:32:00","context":"快件在【淄博市桓台縣工業街營業點】已裝車,准備發往下一站","location":""},{"time":"2017-09-20 13:37:08","ftime":"2017-09-20 13:37:08","context":"快件到達 【淄博市桓台縣工業街營業點】","location":""},{"time":"2017-09-20 10:47:07","ftime":"2017-09-20 10:47:07","context":"快件在【淄博高新集散中心】已裝車,准備發往下一站","location":""},{"time":"2017-09-20 10:15:47","ftime":"2017-09-20 10:15:47","context":"快件到達 【淄博高新集散中心】","location":""},{"time":"2017-09-19 23:20:18","ftime":"2017-09-19 23:20:18","context":"快件在【深圳總集散中心】已裝車,准備發往下一站","location":""},{"time":"2017-09-19 22:39:27","ftime":"2017-09-19 22:39:27","context":"快件到達 【深圳總集散中心】","location":""},{"time":"2017-09-19 18:57:33","ftime":"2017-09-19 18:57:33","context":"快件在【深圳龍華新區華聯社區營業部】已裝車,准備發往下一站","location":""},{"time":"2017-09-19 16:12:21","ftime":"2017-09-19 16:12:21","context":"順豐速運 已收取快件","location":""}]}
上面是我們在上一篇文章中請求返回來的JSON字符串,那么我們現在要解析他。第一步就是要根據這個JSON來寫出對應的實體類。用來存放數據。這個實體類如何寫的?其實非常簡單。因為一般
不需要手動自己寫,當然,你要是喜歡也可以自己寫。不過我一般使用網站直接轉換。自己百度 查一下,JSON轉C#實體類,就會有很多網站給你轉。
我使用的是這個網站:http://www.bejson.com/convert/json2csharp/
使用很簡單,把JSON放進去,點擊生成就可以自動生成一個實體類。其實是兩個類,不過一般我們寫在一個文件里。
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace WindowsFormsApplication1 { /// <summary> /// JSON數據的實體類 /// </summary> public class Root { /// <summary> /// /// </summary> public string message { get; set; } /// <summary> /// /// </summary> public string nu { get; set; } /// <summary> /// /// </summary> public string ischeck { get; set; } /// <summary> /// /// </summary> public string condition { get; set; } /// <summary> /// /// </summary> public string com { get; set; } /// <summary> /// /// </summary> public string status { get; set; } /// <summary> /// /// </summary> public string state { get; set; } /// <summary> /// /// </summary> public List<DataItem> data { get; set; } } public class DataItem { /// <summary> /// /// </summary> public string time { get; set; } /// <summary> /// /// </summary> public string ftime { get; set; } /// <summary> /// 已簽收,感謝使用順豐,期待再次為您服務 /// </summary> public string context { get; set; } /// <summary> /// /// </summary> public string location { get; set; } } }
實體類創建好后,我們還需要一個DLL文件,Newtonsoft.Json.DLL,這個文件哪里來呢?很簡單,百度一下不就來了。。。。這個DLL的官方網站是:https://www.newtonsoft.com/json
下載下來后,引入,引用(這兩個步驟就不需要我教了吧~不懂就百度~)
做完這准備工作后,就進入大家最喜歡的寫代碼環節了。非常簡單,一句代碼搞定。自己看吧!
PS,我們接着使用上一篇文章用到的項目,添加一個按鈕,在按鈕里面寫事件。代碼如下:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using Newtonsoft.Json; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { //我們的接口 string url = "http://www.kuaidi100.com/query?type=shunfeng&postid=367847964498"; //將接口傳入,這個HttpUitls的類,有興趣可以研究下,也可以直接用就可以,不用管如何實現。 string getJson = HttpUitls.Get(url); MessageBox.Show(getJson); } private void button2_Click(object sender, EventArgs e) { //我們的接口 string url = "http://www.kuaidi100.com/query?type=shunfeng&postid=367847964498"; //將接口傳入,這個HttpUitls的類,有興趣可以研究下,也可以直接用就可以,不用管如何實現。 string getJson = HttpUitls.Get(url); //這個需要引入Newtonsoft.Json這個DLL並using //傳入我們的實體類還有需要解析的JSON字符串這樣就OK了。然后就可以通過實體類使用數據了。 Root rt = JsonConvert.DeserializeObject<Root>(getJson); //這樣就可以取出json數據里面的值 MessageBox.Show("com=" + rt.com + "\r\n" + "condition=" + rt.condition + "\r\n" + "ischeck=" + rt.ischeck + "\r\n" + "state=" + rt.state + "\r\n" + "status=" + rt.status); //由於這個JSON字符串的 public List<DataItem> data 是一個集合,所以我們需要遍歷集合里面的所有數據 for (int i = 0; i < rt.data.Count; i++) { MessageBox.Show("Data=" + rt.data[i].context + "\r\n" + rt.data[i].location + "\r\n" + rt.data[i].time + "\r\n" + rt.data[i].ftime); } } } }