最新推荐文章于 2024-07-31 23:37:13 发布
Hommmmmmm 于 2021-07-30 08:36:35 发布
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
/// <summary>
/// JArray转DataTable
/// </summary>
/// <param name="dataArr"></param>
/// <returns></returns>
public static DataTable ConvertToDataTable(JArray dataArr)
{
if (dataArr == null || dataArr.Count <= 0)
return null;
DataTable result = new DataTable();
var colnames = ((JObject)(dataArr.First)).Properties();
List<string> columnNames = new List<string>();
if (colnames == null || colnames.Count() <= 0)
return null;
foreach (var item in colnames)
{
if (!columnNames.Contains(item.Name))
columnNames.Add(item.Name);
result.Columns.Add(item.Name, typeof(string));
}
foreach (JObject data in dataArr)
{
JObject jo = JObject.Parse(data.ToString());
DataRow row = result.NewRow();
foreach (var columnName in columnNames)
{
if (jo.Property(columnName) == null)
{
data.Add(columnName, "");
row[columnName] = data[columnName].ToString();
}
else
{
row[columnName] = data[columnName].ToString();
}
}
result.Rows.Add(row);
}
return result;
}