.net C# json转DataTable
程序员文章站
2022-06-13 21:14:49
...
public DataTable JsonToDataTable(string json)
{
DataTable dataTable = new DataTable(); //实例化
DataTable result;
try
{
JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
javaScriptSerializer.MaxJsonLength = Int32.MaxValue; //取得最大数值
ArrayList arrayList = javaScriptSerializer.Deserialize<ArrayList>(json);
if (arrayList.Count > 0)
{
foreach (Dictionary<string, object> dictionary in arrayList)
{
if (dictionary.Keys.Count<string>() == 0)
{
result = dataTable;
return result;
}
if (dataTable.Columns.Count == 0)
{
foreach (string current in dictionary.Keys)
{
dataTable.Columns.Add(current, dictionary[current].GetType());
}
}
DataRow dataRow = dataTable.NewRow();
foreach (string current in dictionary.Keys)
{
dataRow[current] = dictionary[current];
}
dataTable.Rows.Add(dataRow); //循环添加行到DataTable中
}
}
}
catch
{
}
result = dataTable;
return result;
}
推荐阅读
-
C#中的DataSet、string、DataTable、对象转换成Json的实现代码
-
C#实现Json转Unicode的方法
-
c#的datatable转list示例
-
C#中把Datatable转换为Json的5个代码实例
-
C#中的DataSet、string、DataTable、对象转换成Json的实现代码
-
C#实现Json转Unicode的方法
-
C#中DataTable 转实体实例详解
-
Asp.net_静态方法之Grid转DataTable方法分享
-
C# convert json to datatable,convert list to datatable
-
C#使用Json.Net进行序列化和反序列化及定制化