C#解析JSON数组
C#解析JSON数组
方式一
第一步:使用前,需下载:Newtonsoft.Json.dll
没有的,请到我百度云盘下载
链接:https://pan.baidu.com/s/1JBkee4qhtW7XOyYFiGOL2Q
提取码:b5uq
第二步:引入命名空间:using Newtonsoft.Json;
第三步:封装一个函数,方便以后使用
待解析JSON数组
函数:
1 public static Newtonsoft.Json.Linq.JArray GetToJsonList(string json)
2 {
3 Newtonsoft.Json.Linq.JArray jsonArr = (Newtonsoft.Json.Linq.JArray)JsonConvert.DeserializeObject(json);
4 return jsonArr;
5 }
实现:
搞定~
方式二(推荐):
第一步就是要根据这个JSON来写出对应的实体类。用来存放数据。这个实体类如何写的?其实非常简单。因为一般不需要手动自己写,当然,你要是喜欢也可以自己写。不过我一般使用网站直接转换。自己百度 查一下,JSON转C#实体类,就会有很多网站给你转。
我使用的是这个网站:http://www.bejson.com/convert/json2csharp/
{"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放到这个网站,自动给我们生成实体类即可
实体类:
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace WindowsFormsApplication1
7 {
8
9 /// <summary>
10 /// JSON数据的实体类
11 /// </summary>
12 public class Root
13 {
14 /// <summary>
15 ///
16 /// </summary>
17 public string message { get; set; }
18 /// <summary>
19 ///
20 /// </summary>
21 public string nu { get; set; }
22 /// <summary>
23 ///
24 /// </summary>
25 public string ischeck { get; set; }
26 /// <summary>
27 ///
28 /// </summary>
29 public string condition { get; set; }
30 /// <summary>
31 ///
32 /// </summary>
33 public string com { get; set; }
34 /// <summary>
35 ///
36 /// </summary>
37 public string status { get; set; }
38 /// <summary>
39 ///
40 /// </summary>
41 public string state { get; set; }
42 /// <summary>
43 ///
44 /// </summary>
45 public List<DataItem> data { get; set; }
46 }
47 public class DataItem
48 {
49 /// <summary>
50 ///
51 /// </summary>
52 public string time { get; set; }
53 /// <summary>
54 ///
55 /// </summary>
56 public string ftime { get; set; }
57 /// <summary>
58 /// 已签收,感谢使用顺丰,期待再次为您服务
59 /// </summary>
60 public string context { get; set; }
61 /// <summary>
62 ///
63 /// </summary>
64 public string location { get; set; }
65 }
66
67 }
实体类创建好后,我们还需要一个DLL文件,Newtonsoft.Json.DLL,看方式一
封装一个方法
1 /// <summary>
2 /// 将JSON转字符串(包括数组)
3 /// </summary>
4 /// <typeparam name="T"></typeparam>
5 /// <param name="json"></param>
6 /// <returns></returns>
7 public static T JsonConvertObject<T>(string json)
8 {
9 return JsonConvert.DeserializeObject<T>(json);
10 }
调用即可
以下JSON帮助类
1 using System.Collections.Generic;
2 using System.IO;
3 using Newtonsoft.Json;
4 using Newtonsoft.Json.Linq;
5 using System.Data;
6 using System.Reflection;
7 using System;
8
9 namespace Sam.OA.Common
10 {
11 /// <summary>
12 /// Json帮助类
13 /// 使用前需引用开源项目类库:Newtonsoft.Json.dll
14 /// </summary>
15 public sealed class JsonHelper
16 {
17 /// <summary>
18 /// 将对象序列化为json格式
19 /// </summary>
20 /// <param name="obj">序列化对象</param>
21 /// <returns>json字符串</returns>
22 public static string SerializeObjct(object obj)
23 {
24 return JsonConvert.SerializeObject(obj);
25 }
26 /// <summary>
27 /// 解析JSON字符串生成对象实体
28 /// </summary>
29 /// <typeparam name="T">实体类</typeparam>
30 /// <param name="json">JSON字符串</param>
31 /// <returns></returns>
32 public static T JsonConvertObject<T>(string json)
33 {
34 return JsonConvert.DeserializeObject<T>(json);
35 }
36 /// <summary>
37 /// 解析JSON字符串生成对象实体
38 /// </summary>
39 /// <typeparam name="T">对象类型</typeparam>
40 /// <param name="json">json字符串</param>
41 /// <returns></returns>
42 public static T DeserializeJsonToObject<T>(string json) where T:class
43 {
44 JsonSerializer serializer = new JsonSerializer();
45 StringReader sr = new StringReader(json);
46 object obj = serializer.Deserialize(new JsonTextReader(sr), typeof(T));
47 T t = obj as T;
48 return t;
49 }
50 /// <summary>
51 /// 解析JSON数组生成对象实体集合
52 /// </summary>
53 /// <typeparam name="T">对象类型</typeparam>
54 /// <param name="json">json数组</param>
55 /// <returns>对象实体集合</returns>
56 public static List<T> DeserializeJsonToList<T>(string json) where T : class
57 {
58 JsonSerializer serializer = new JsonSerializer();
59 StringReader sr = new StringReader(json);
60 object obj = serializer.Deserialize(new JsonTextReader(sr), typeof(List<T>));
61 List<T> list = obj as List<T>;
62 return list;
63 }
64 /// <summary>
65 /// 将JSON转数组
66 /// 用法:jsonArr[0]["xxxx"]
67 /// </summary>
68 /// <param name="json">json字符串</param>
69 /// <returns></returns>
70 public static JArray GetToJsonList(string json)
71 {
72 JArray jsonArr = (JArray)JsonConvert.DeserializeObject(json);
73 return jsonArr;
74 }
75 /// <summary>
76 /// 将DataTable转换成实体类
77 /// </summary>
78 /// <typeparam name="T">实体类</typeparam>
79 /// <param name="dt">DataTable</param>
80 /// <returns></returns>
81 public static List<T> DtConvertToModel<T>(DataTable dt) where T : new()
82 {
83 List<T> ts = new List<T>();
84 foreach (DataRow dr in dt.Rows)
85 {
86 T t = new T();
87 foreach (PropertyInfo pi in t.GetType().GetProperties())
88 {
89 if (dt.Columns.Contains(pi.Name))
90 {
91 if (!pi.CanWrite) continue;
92 var value = dr[pi.Name];
93 if (value != DBNull.Value)
94 {
95 switch (pi.PropertyType.FullName)
96 {
97 case "System.Decimal":
98 pi.SetValue(t, decimal.Parse(value.ToString()), null);
99 break;
100 case "System.String":
101 pi.SetValue(t, value.ToString(), null);
102 break;
103 case "System.Int32":
104 pi.SetValue(t, int.Parse(value.ToString()), null);
105 break;
106 default:
107 pi.SetValue(t, value, null);
108 break;
109 }
110 }
111 }
112 }
113 ts.Add(t);
114 }
115 return ts;
116 }
117 }
118 }