在上一篇文章中,我们讲解了如何通过API接口获取返回的JSON字符串,那么,这篇文章我们来讲解拿到了返回的JSON字符串后,我们要如何取到里面我们需要的数据呢?这操作叫JSON的反序列化操作。接下里我们将一一解释。
    先看效果:这个大家最喜欢。
    C#如何解析JSON数据?(反序列化对象) - 图1
    我们先看一下上一篇文章中返回的字符串。

    1. {
    2. "message":"ok",
    3. "nu":"367847964498",
    4. "ischeck":"1",
    5. "condition":"F00",
    6. "com":"shunfeng",
    7. "status":"200",
    8. "state":"3",
    9. "data":[{"time":"2017-09-21 09:33:09",
    10. "ftime":"2017-09-21 09:33:09",
    11. "context":"已签收,感谢使用顺丰,期待再次为您服务",
    12. "location":""},
    13. {"time":"2017-09-21 09:09:48",
    14. "ftime":"2017-09-21 09:09:48",
    15. "context":"快件交给巩向涛,正在派送途中(联系电话:18806439871)",
    16. "location":""},
    17. {"time":"2017-09-21 07:02:41",
    18. "ftime":"2017-09-21 07:02:41",
    19. "context":"快件到达 【淄博市桓台田庄速运营业点 】",
    20. "location":""},
    21. {"time":"2017-09-20 15:32:00",
    22. "ftime":"2017-09-20 15:32:00",
    23. "context":"快件在【淄博市桓台县工业街营业点】已装车,准备发往下一站",
    24. "location":""},
    25. {"time":"2017-09-20 13:37:08",
    26. "ftime":"2017-09-20 13:37:08",
    27. "context":"快件到达 【淄博市桓台县工业街营业点】",
    28. "location":""},
    29. {"time":"2017-09-20 10:47:07",
    30. "ftime":"2017-09-20 10:47:07",
    31. "context":"快件在【淄博高新集散中心】已装车,准备发往下一站",
    32. "location":""},
    33. {"time":"2017-09-20 10:15:47",
    34. "ftime":"2017-09-20 10:15:47",
    35. "context":"快件到达 【淄博高新集散中心】",
    36. "location":""},
    37. {"time":"2017-09-19 23:20:18",
    38. "ftime":"2017-09-19 23:20:18",
    39. "context":"快件在【深圳总集散中心】已装车,准备发往下一站",
    40. "location":""},
    41. {"time":"2017-09-19 22:39:27",
    42. "ftime":"2017-09-19 22:39:27",
    43. "context":"快件到达 【深圳总集散中心】",
    44. "location":""},
    45. {"time":"2017-09-19 18:57:33",
    46. "ftime":"2017-09-19 18:57:33",
    47. "context":"快件在【深圳龙华新区华联社区营业部】已装车,准备发往下一站",
    48. "location":""},
    49. {"time":"2017-09-19 16:12:21",
    50. "ftime":"2017-09-19 16:12:21",
    51. "context":"顺丰速运 已收取快件",
    52. "location":""}]
    53. }

    上面是我们在上一篇文章中请求返回来的JSON字符串,那么我们现在要解析他。第一步就是要根据这个JSON来写出对应的实体类。用来存放数据。这个实体类如何写的?其实非常简单。因为一般
    不需要手动自己写,当然,你要是喜欢也可以自己写。不过我一般使用网站直接转换。自己百度 查一下,JSON转C#实体类,就会有很多网站给你转。
    我使用的是这个网站:http://www.bejson.com/convert/json2csharp/
    使用很简单,把JSON放进去,点击生成就可以自动生成一个实体类。其实是两个类,不过一般我们写在一个文件里。

    1. using System;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. using System.Text;
    5. namespace WindowsFormsApplication1
    6. {
    7. /// <summary>
    8. /// JSON数据的实体类
    9. /// </summary>
    10. public class Root
    11. {
    12. /// <summary>
    13. ///
    14. /// </summary>
    15. public string message { get; set; }
    16. /// <summary>
    17. ///
    18. /// </summary>
    19. public string nu { get; set; }
    20. /// <summary>
    21. ///
    22. /// </summary>
    23. public string ischeck { get; set; }
    24. /// <summary>
    25. ///
    26. /// </summary>
    27. public string condition { get; set; }
    28. /// <summary>
    29. ///
    30. /// </summary>
    31. public string com { get; set; }
    32. /// <summary>
    33. ///
    34. /// </summary>
    35. public string status { get; set; }
    36. /// <summary>
    37. ///
    38. /// </summary>
    39. public string state { get; set; }
    40. /// <summary>
    41. ///
    42. /// </summary>
    43. public List<DataItem> data { get; set; }
    44. }
    45. public class DataItem
    46. {
    47. /// <summary>
    48. ///
    49. /// </summary>
    50. public string time { get; set; }
    51. /// <summary>
    52. ///
    53. /// </summary>
    54. public string ftime { get; set; }
    55. /// <summary>
    56. /// 已签收,感谢使用顺丰,期待再次为您服务
    57. /// </summary>
    58. public string context { get; set; }
    59. /// <summary>
    60. ///
    61. /// </summary>
    62. public string location { get; set; }
    63. }
    64. }

    实体类创建好后,我们还需要一个DLL文件,Newtonsoft.Json.DLL,这个文件哪里来呢?很简单,百度一下不就来了。。。。这个DLL的官方网站是:https://www.newtonsoft.com/json
    下载下来后,引入,引用(这两个步骤就不需要我教了吧~不懂就百度~)
    做完这准备工作后,就进入大家最喜欢的写代码环节了。非常简单,一句代码搞定。自己看吧!
    PS,我们接着使用上一篇文章用到的项目,添加一个按钮,在按钮里面写事件。代码如下:

    1. using System;
    2. using System.Collections.Generic;
    3. using System.ComponentModel;
    4. using System.Data;
    5. using System.Drawing;
    6. using System.Linq;
    7. using System.Text;
    8. using System.Windows.Forms;
    9. using Newtonsoft.Json;
    10. namespace WindowsFormsApplication1
    11. {
    12. public partial class Form1 : Form
    13. {
    14. public Form1()
    15. {
    16. InitializeComponent();
    17. }
    18. private void button1_Click(object sender, EventArgs e)
    19. {
    20. //我们的接口
    21. string url = "http://www.kuaidi100.com/query?type=shunfeng&postid=367847964498";
    22. //将接口传入,这个HttpUitls的类,有兴趣可以研究下,也可以直接用就可以,不用管如何实现。
    23. string getJson = HttpUitls.Get(url);
    24. MessageBox.Show(getJson);
    25. }
    26. private void button2_Click(object sender, EventArgs e)
    27. {
    28. //我们的接口
    29. string url = "http://www.kuaidi100.com/query?type=shunfeng&postid=367847964498";
    30. //将接口传入,这个HttpUitls的类,有兴趣可以研究下,也可以直接用就可以,不用管如何实现。
    31. string getJson = HttpUitls.Get(url);
    32. //这个需要引入Newtonsoft.Json这个DLL并using
    33. //传入我们的实体类还有需要解析的JSON字符串这样就OK了。然后就可以通过实体类使用数据了。
    34. Root rt = JsonConvert.DeserializeObject<Root>(getJson);
    35. //这样就可以取出json数据里面的值
    36. 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);
    37. //由于这个JSON字符串的 public List<DataItem> data 是一个集合,所以我们需要遍历集合里面的所有数据
    38. for (int i = 0; i < rt.data.Count; i++)
    39. {
    40. 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);
    41. }
    42. }
    43. }
    44. }