C#Json转DataTable
2021-06-26 19:06
标签:ict string serialize column reac ToJson cti str new /// public static string ToJson(this DataTable dt) return javaScriptSerializer.Serialize(arrayList); //返回一个json字符串 C#Json转DataTable 标签:ict string serialize column reac ToJson cti str new 原文地址:https://www.cnblogs.com/robertyao/p/10092368.html
/// Json 字符串 转换为 DataTable数据集合
///
///
///
public static DataTable ToDataTableTwo(string json)
{
DataTable dataTable = new DataTable(); //实例化
DataTable result;
try
{
JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
javaScriptSerializer.MaxJsonLength = Int32.MaxValue; //取得最大数值
ArrayList arrayList = javaScriptSerializer.Deserialize(json);
if (arrayList.Count > 0)
{
foreach (Dictionary
{
if (dictionary.Keys.Count
{
result = dataTable;
return result;
}
//Columns
if (dataTable.Columns.Count == 0)
{
foreach (string current in dictionary.Keys)
{
dataTable.Columns.Add(current, dictionary[current].GetType());
}
}
//Rows
DataRow dataRow = dataTable.NewRow();
foreach (string current in dictionary.Keys)
{
dataRow[current] = dictionary[current];
}
dataTable.Rows.Add(dataRow); //循环添加行到DataTable中
}
}
}
catch
{
}
result = dataTable;
return result;
}
{
JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
javaScriptSerializer.MaxJsonLength = Int32.MaxValue; //取得最大数值
ArrayList arrayList = javaScriptSerializer.Deserialize(json);
foreach (DataRow dataRow in dt.Rows)
{
Dictionary
foreach (DataColumn dataColumn in dt.Columns)
{
dictionary.Add(dataColumn.ColumnName, dataRow[dataColumn.ColumnName].ToStr());
}
arrayList.Add(dictionary); //ArrayList集合中添加键值
}
}