实体类Json串转成DataTable

private DataTable GetJsonToDataTable(string json)
{
List arrayList = JsonConvert.DeserializeObject>(json);
if (arrayList.Count > 0)
{
DataTable dataTable = new DataTable();
//循环类属性
Type type = arrayList[0].GetType();
PropertyInfo[] propertyInfos = type.GetProperties();
foreach (var item in propertyInfos)
{
dataTable.Columns.Add(item.Name);
}
//循环类属性值
foreach (Object_DeclareInfo item in arrayList)
{
DataRow dataRow = dataTable.NewRow();
foreach (PropertyInfo item1 in propertyInfos)
{
dataRow[item1.Name] = (item1.GetValue(item, null) == null ? "" : item1.GetValue(item, null)).ToString();
}
dataTable.Rows.Add(dataRow);
}
return dataTable;
}
return null;
}

你可能感兴趣的:(实体类Json串转成DataTable)