C#解析JSON数组

时间:2021-08-22 09:49:28

方式一

第一步:使用前,需下载:Newtonsoft.Json.dll

没有的,请到我百度云盘下载

链接:https://pan.baidu.com/s/1JBkee4qhtW7XOyYFiGOL2Q 
提取码:b5uq

第二步:引入命名空间:using Newtonsoft.Json;

第三步:封装一个函数,方便以后使用

待解析JSON数组

C#解析JSON数组

函数:

         public static Newtonsoft.Json.Linq.JArray GetToJsonList(string json)
{
Newtonsoft.Json.Linq.JArray jsonArr = (Newtonsoft.Json.Linq.JArray)JsonConvert.DeserializeObject(json);
return jsonArr;
}

实现:

C#解析JSON数组

搞定~

方式二(推荐):

  第一步就是要根据这个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放到这个网站,自动给我们生成实体类即可

实体类:

 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,看方式一

封装一个方法

         /// <summary>
/// 将JSON转字符串(包括数组)
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="json"></param>
/// <returns></returns>
public static T JsonConvertObject<T>(string json)
{
return JsonConvert.DeserializeObject<T>(json);
}

调用即可

以下JSON帮助类

 using System.Collections.Generic;
using System.IO;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Data;
using System.Reflection;
using System; namespace Sam.OA.Common
{
/// <summary>
/// Json帮助类
/// 使用前需引用开源项目类库:Newtonsoft.Json.dll
/// </summary>
public sealed class JsonHelper
{
/// <summary>
/// 将对象序列化为json格式
/// </summary>
/// <param name="obj">序列化对象</param>
/// <returns>json字符串</returns>
public static string SerializeObjct(object obj)
{
return JsonConvert.SerializeObject(obj);
}
/// <summary>
/// 解析JSON字符串生成对象实体
/// </summary>
/// <typeparam name="T">实体类</typeparam>
/// <param name="json">JSON字符串</param>
/// <returns></returns>
public static T JsonConvertObject<T>(string json)
{
return JsonConvert.DeserializeObject<T>(json);
}
/// <summary>
/// 解析JSON字符串生成对象实体
/// </summary>
/// <typeparam name="T">对象类型</typeparam>
/// <param name="json">json字符串</param>
/// <returns></returns>
public static T DeserializeJsonToObject<T>(string json) where T:class
{
JsonSerializer serializer = new JsonSerializer();
StringReader sr = new StringReader(json);
object obj = serializer.Deserialize(new JsonTextReader(sr), typeof(T));
T t = obj as T;
return t;
}
/// <summary>
/// 解析JSON数组生成对象实体集合
/// </summary>
/// <typeparam name="T">对象类型</typeparam>
/// <param name="json">json数组</param>
/// <returns>对象实体集合</returns>
public static List<T> DeserializeJsonToList<T>(string json) where T : class
{
JsonSerializer serializer = new JsonSerializer();
StringReader sr = new StringReader(json);
object obj = serializer.Deserialize(new JsonTextReader(sr), typeof(List<T>));
List<T> list = obj as List<T>;
return list;
}
/// <summary>
/// 将JSON转数组
/// 用法:jsonArr[0]["xxxx"]
/// </summary>
/// <param name="json">json字符串</param>
/// <returns></returns>
public static JArray GetToJsonList(string json)
{
JArray jsonArr = (JArray)JsonConvert.DeserializeObject(json);
return jsonArr;
}
/// <summary>
/// 将DataTable转换成实体类
/// </summary>
/// <typeparam name="T">实体类</typeparam>
/// <param name="dt">DataTable</param>
/// <returns></returns>
public static List<T> DtConvertToModel<T>(DataTable dt) where T : new()
{
List<T> ts = new List<T>();
foreach (DataRow dr in dt.Rows)
{
T t = new T();
foreach (PropertyInfo pi in t.GetType().GetProperties())
{
if (dt.Columns.Contains(pi.Name))
{
if (!pi.CanWrite) continue;
var value = dr[pi.Name];
if (value != DBNull.Value)
{
switch (pi.PropertyType.FullName)
{
case "System.Decimal":
pi.SetValue(t, decimal.Parse(value.ToString()), null);
break;
case "System.String":
pi.SetValue(t, value.ToString(), null);
break;
case "System.Int32":
pi.SetValue(t, int.Parse(value.ToString()), null);
break;
default:
pi.SetValue(t, value, null);
break;
}
}
}
}
ts.Add(t);
}
return ts;
}
}
}