json转datatable
public static DataTable JsonToDataTable(string strJson)
{
DataTable tb = null;
//获取数据
Regex rg = new Regex(@"(?<={)[^}]+(?=})");
MatchCollection mc = rg.Matches(strJson);
for (int i = 0; i < mc.Count; i++)
{
string strRow = mc[i].Value;
string[] strRows = strRow.Split(',');
//创建表
if (tb == null)
{
tb = new DataTable();
tb.TableName = "";
foreach (string str in strRows)
{
DataColumn dc = new DataColumn();
string[] strCell = str.Replace("{","").Replace("[","").Split(':');
dc.ColumnName = strCell[0].ToString().Replace("\"", "").Trim();
tb.Columns.Add(dc);
}
tb.AcceptChanges();
}
//增加内容
DataRow dr = tb.NewRow();
for (int r = 0; r < strRows.Length; r++)
{
dr[r] = strRows[r].Split(new char[2]{'\\','"'})[3].Trim().Replace(",", ",").Replace(":", ":").Replace("\"", "").Trim();
}
tb.Rows.Add(dr);
tb.AcceptChanges();
}
return tb;
}