分享

C#解析JSON数据

 朝花夕拾and 2020-03-13

第一章: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./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./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); } } }}
复制代码

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约