C# json字符串转DataTable
2020-12-13 14:22
标签:des style blog http io color os ar java 我的json格式是这样的: 转出来的结果: 代码: 1. 需要引用 Newtonsoft.Json.dll , 可以去网上下载 2.引入System.Web.Extensions 组件 引用 : using System.Text.RegularExpressions; 3. 方法ToDataTable C# json字符串转DataTable 标签:des style blog http io color os ar java 原文地址:http://www.cnblogs.com/Marvellous/p/4062465.html{"success":true,"data":[{"C4":"英语","C3":"","C1":"99","C2":"CCC"},{"C4":"数学","C3":"","C1":"55","C2":"DDD"}]}
using System.Web.Script.Serialization;public DataTable JsonToDataTable(string strJson)
{
DataTable dt =null ;
try
{
JObject jo = (JObject)JsonConvert.DeserializeObject(strJson);
bool success = (bool)jo["success"];
if (!success)
{
return null;
}
JArray ja = (JArray)jo["data"];
dt = ToDataTable(ja.ToString());
}
catch (Exception ex)
{
return null;
}
return dt;
}
public DataTable ToDataTable( string json)
{
DataTable dataTable = new DataTable(); //实例化
DataTable result;
JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
javaScriptSerializer.MaxJsonLength = Int32.MaxValue; //取得最大数值
ArrayList arrayList = javaScriptSerializer.Deserialize(json);
if (arrayList.Count > 0)
{
foreach (Dictionarystring, object> dictionary in arrayList)
{
//if (dictionary.Keys.Count