码迷,mamicode.com
首页 > Web开发 > 详细

json转datatable

时间:2017-09-30 20:00:22      阅读:269      评论:0      收藏:0      [点我收藏+]

标签:ati   return   new   hang   count   foreach   tab   regex   reac   

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;
}

json转datatable

标签:ati   return   new   hang   count   foreach   tab   regex   reac   

原文地址:http://www.cnblogs.com/axin85/p/7615542.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!