/// <summary>
/// Json 字符串 转换为 DataTable数据集合
/// </summary>
/// <param name="jsonStr">Json格式 字符串</param>
/// <returns>DataTable数据集合</returns>
public static DataTable JsonToDataTable(string jsonStr, string wellName, string url)
{
DataTable dataTable = new DataTable(); //实例化
DataTable result;
if (jsonStr == string.Empty || jsonStr == "")
{
result = dataTable;
return result;
}
try
{
JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
//javaScriptSerializer.MaxJsonLength = Int32.MaxValue; //取得最大数值
// 对jsonStr 进行多层解析
JObject jo = (JObject)JsonConvert.DeserializeObject(jsonStr);
string datas = jo["data"][0]["datas"].ToString();
ArrayList arrayList = javaScriptSerializer.Deserialize<ArrayList>(datas);
string fields = jo["data"][0]["fields"].ToString();
fields = "[" + fields + "]";
ArrayList fieldsList = javaScriptSerializer.Deserialize<ArrayList>(fields);
if (arrayList.Count > 0)
{
//Columns
if (fieldsList.Count > 0)
{
if (dataTable.Columns.Count == 0)
foreach (Dictionary<string, object> dict in fieldsList)
{
foreach (string item in dict.Keys)
{
if (item == "id")
{
dataTable.Columns.Add("ids", GetTypeByString(dict[item].ToString()));
// dataTable.Columns.Add("ids", typeof(string));
}
else
{
string a = dict[item].ToString();
dataTable.Columns.Add(item, GetTypeByString(dict[item].ToString()));
//Console.WriteLine(dictionary[current].GetType());
// dataTable.Columns.Add(current, typeof(string));
}
}
}
foreach (Dictionary<string, object> dictionary in arrayList)
{
if (dictionary.Keys.Count<string>() == 0)
{
result = dataTable;
return result;
}
//Rows
DataRow dataRow = dataTable.NewRow();
foreach (string current in dictionary.Keys)
{
if (dictionary[current].ToString().Trim() != "")
{
if (current == "id")
{
//dataRow["ids"] = dictionary[current].ToString().Trim();
dataRow["ids"] = dictionary[current];
}
else
{
// dataRow[current] = dictionary[current].ToString().Trim();
dataRow[current] = dictionary[current];
}
}
}
dataTable.Rows.Add(dataRow); //循环添加行到DataTable中
}
}
}
}
catch (Exception ex)
{
Console.WriteLine(wellName + url + "jsonStr转Tbale出现异常: " + ex.Message + " jsonStr:" + jsonStr);
Console.WriteLine(wellName + url + "jsonStr转Tbale出现异常: " + ex.Message + " jsonStr:" + jsonStr);
}
result = dataTable;
return result;
}
public static Type GetTypeByString(string type)
{
switch (type.ToLower())
{
case "bool":
return Type.GetType("System.Boolean", true, true);
case "byte":
return Type.GetType("System.Byte", true, true);
case "sbyte":
return Type.GetType("System.SByte", true, true);
case "char":
return Type.GetType("System.Char", true, true);
case "decimal":
return Type.GetType("System.Decimal", true, true);
case "double":
return Type.GetType("System.Double", true, true);
case "float":
return Type.GetType("System.Single", true, true);
case "int":
return Type.GetType("System.Int32", true, true);
case "uint":
return Type.GetType("System.UInt32", true, true);
case "long":
return Type.GetType("System.Int64", true, true);
case "ulong":
return Type.GetType("System.UInt64", true, true);
case "object":
return Type.GetType("System.Object", true, true);
case "short":
return Type.GetType("System.Int16", true, true);
case "ushort":
return Type.GetType("System.UInt16", true, true);
case "string":
return Type.GetType("System.String", true, true);
case "date":
case "datetime":
return Type.GetType("System.DateTime", true, true);
case "guid":
return Type.GetType("System.Guid", true, true);
default:
return Type.GetType("System.String", true, true);
}
}