美文网首页
c#解析json

c#解析json

作者: 启蒙时代 | 来源:发表于2019-12-21 18:29 被阅读0次

    json是一种通用的文本序列,有其固定的格式。 json的使用前必须转化为实体类。转化方法是在线转换。网址:http://www.bejson.com/convert/json2csharp/

    一,格式1及实体类

    {"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":""}]}

    特征:

    1,由大括号{}首尾包围

    2,包含两大部分,第一部分为头部,有一些属性值。第二部分是正文,命名为data,由中括号[]包围。

    转化为实体类:

    public class DataItem{ /// /// /// public string time { get; set; }

    ///     ///    ///     public string ftime { get; set; }

    ///     /// 已签收,感谢使用顺丰,期待再次为您服务    ///     public string context { get; set; }

    ///     ///    ///     public string location { get; set; }

    }

    public class Root{    ///     ///    ///     public string message { get; set; }

    ///     ///    ///     public string nu { get; set; }

    ///     ///    ///     public string ischeck { get; set; }

    ///     ///    ///     public string condition { get; set; }

    ///     ///    ///     public string com { get; set; }

    ///     ///    ///     public string status { get; set; }

    ///     ///    ///     public string state { get; set; }

    ///     ///    ///     public List data { get; set; }

    }

    特征:

    1,头部类:Root,结尾处定义了其表示方法:data

    2,主体类,从json中提取

    二、格式2及实体类

    {"log_id": 6822962530653775861, "words_result_num": 2, "words_result": [{"location": {"width": 317, "top": 32, "left": 0, "height": 43}, "words": "第一部分"}, {"location": {"width": 318, "top": 193, "left": 0, "height": 45}, "words": "第二部分"}]}

    除具备格式1的特征外,发现主体部分命名为words_result_num,有两个值分别是location和words,其第一个值又是一个新的集合。

    public class Location{ /// /// /// public int width { get; set; }

    ///     ///    ///     public int top { get; set; }

    ///     ///    ///     public int left { get; set; }

    ///     ///    ///     public int height { get; set; }

    }

    public class Words_resultItem{    ///     ///    ///     public Location location { get; set; }

    ///     /// 第一部分    ///     public string words { get; set; }

    }

    public class Root{    ///     ///    ///     public int log_id { get; set; }

    ///     ///    ///     public int words_result_num { get; set; }

    ///     ///    ///     public List words_result { get; set; }

    }

    发现,分成三个类。

    二、添加一个类文件

    在项目中添加一个类json1,使用转换后的实体类,替换了:

    class Json1

        {

        }

    这时,无需引用,这个类文件即可被主程序调用。

    三、声明dll

    下载 Newtonsoft.Json.dll,并声明

    using Newtonsoft.Json;

    四、使用

    格式1示例:

    Root rt = JsonConvert.DeserializeObject<Root>(JsonTxt);

                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);

                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);

                }

    格式2示例:

    Root rt = JsonConvert.DeserializeObject<Root>(getJsontxt);

                for (int i = 0; i < rt.words_result.Count; i++)

                {

                    MessageBox.Show("Data=" + rt.words_result[i].words + "\r\n"+ rt.words_result[i].location.top);

                }

    需要说明的是,示例2会报int32越界的错误,因为有一个整数型的值太大了,需要提前处理,将数值改小即可。

    相关文章

      网友评论

          本文标题:c#解析json

          本文链接:https://www.haomeiwen.com/subject/fvignctx.html