public string JsonToTable(string json,string type)
{
string a = "";
DataTable dTb = new DataTable();
DataTable result;//处理json数据
if(type == "1")
#region
{
try
{
JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
javaScriptSerializer.MaxJsonLength = Int32.MaxValue; //取得最大数值
ArrayList arrayList = javaScriptSerializer.Deserialize<ArrayList>(json);
if (arrayList.Count > 0)
{
foreach (Dictionary<string, object> dictionary in arrayList)
{
if (dictionary.Keys.Count<string>() == 0)
{
result = dTb;
return a;
}
//Columns
if (dTb.Columns.Count == 0)
{
foreach (string current in dictionary.Keys)
{
dTb.Columns.Add(current, dictionary[current].GetType());
}
}
//Rows
DataRow dataRow = dTb.NewRow();
foreach (string current in dictionary.Keys)
{
dataRow[current] = dictionary[current];
}
dTb.Rows.Add(dataRow); //循环添加行到DataTable中
}
}
}
catch { }
}
#endregion
if(type == "2")
#region
{
try
{
JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
javaScriptSerializer.MaxJsonLength = Int32.MaxValue; //取得最大数值
ArrayList arrayList = javaScriptSerializer.Deserialize<ArrayList>(json);
if(arrayList.Count > 0 )
{
foreach (Dictionary<string, object> dictionary in arrayList)
{
if (dictionary.Keys.Count<string>() == 0)
{
result = dTb;
//数据处理
return a;
}
//
if (dTb.Columns.Count == 0)
{
foreach (string current in dictionary.Keys)
{
if (current != "data")
dTb.Columns.Add(current, dictionary[current].GetType());
else
{
ArrayList list = dictionary[current] as ArrayList;
foreach (Dictionary<string, object> dic in list)
{
foreach (string key in dic.Keys)
{
dTb.Columns.Add(key, dic[key].GetType());
}
break;
}
}
}
}
string root = "";
foreach (string current in dictionary.Keys)
{
if (current != "data")
root = current;
else
{
ArrayList list = dictionary[current] as ArrayList;
foreach (Dictionary<string, object> dic in list)
{
DataRow dataRow = dTb.NewRow();
dataRow[root] = dictionary[root];
foreach (string key in dic.Keys)
{
dataRow[key] = dic[key];
}
dTb.Rows.Add(dataRow);
}
}
}
}
}
}
catch
{
}
}
#endregion
if(type == "3")
#region
{
try
{
dTb.Columns.Add("id");
dTb.Columns.Add("mac");
dTb.Columns.Add("rssi");
dTb.Columns.Add("ch");
dTb.Columns.Add("ts");
dTb.Columns.Add("tmc");
dTb.Columns.Add("tc");
dTb.Columns.Add("ds");
dTb.Columns.Add("essid");
JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
javaScriptSerializer.MaxJsonLength = Int32.MaxValue; //取得最大数值
ArrayList arrayList = javaScriptSerializer.Deserialize<ArrayList>(json);
if (arrayList.Count > 0)
{
foreach (Dictionary<string, object> dictionary in arrayList)
{
if (dictionary.Keys.Count<string>() == 0)
{
result = dTb;
return a;
}//Rows
string root = "";
foreach (string current in dictionary.Keys)
{
if (current != "data")
root = current;
else
{
ArrayList list = dictionary[current] as ArrayList;
foreach (Dictionary<string, object> dic in list)
{
DataRow dataRow = dTb.NewRow();
dataRow[root] = dictionary[root];
foreach (string key in dic.Keys)
{
dataRow[key] = dic[key];
}
dTb.Rows.Add(dataRow);
}
}
}
}
}
}
catch
{
}
}
#endregion
result = dTb;
a = DataTableToJson(dTb);
return a;
}
转载于:https://www.cnblogs.com/liarwang/p/10550343.html