#region Json转换dataTable
/// <summary>
/// 将JArray转换为datatable
/// </summary>
/// <param name="dataArr"></param>
/// <returns></returns>
private DataTable JArrayToDatable(JArray dataArr)
{
if (dataArr == null || dataArr.Count <= 0) return null;
DataTable result = new DataTable();
var colnames = ((JObject)dataArr.First).Properties();
List<string> columnNames = new List<string>();
if (colnames == null || colnames.Count() <= 0) return null;
foreach (var item in colnames)
{
if (!columnNames.Contains(item.Name))
{
columnNames.Add(item.Name);
}
result.Columns.Add(item.Name, typeof(string));
}
foreach (JObject data in dataArr)
{
JObject jo = JObject.Parse(data.ToString());
DataRow row = result.NewRow();
foreach (var columnName in columnNames)
{
if (jo.Property(columnName) == null)
{
data.Add(columnName, "");
row[columnName] = data[columnName].ToString();
}
else
{
row[columnName] = data[columnName].ToString();
}
}
result.Rows.Add(row);
}
return result;
}
/// <summary>
/// 将JArray转换为datatable
/// </summary>
/// <param name="dataArr"></param>
/// <returns></returns>
private DataTable JArrayToDatable(JArray dataArr)
{
if (dataArr == null || dataArr.Count <= 0) return null;
DataTable result = new DataTable();
var colnames = ((JObject)dataArr.First).Properties();
List<string> columnNames = new List<string>();
if (colnames == null || colnames.Count() <= 0) return null;
foreach (var item in colnames)
{
if (!columnNames.Contains(item.Name))
{
columnNames.Add(item.Name);
}
if (item.Name == "ShowOrder")
{
result.Columns.Add(item.Name, typeof(int));
}
else
result.Columns.Add(item.Name, typeof(string));
}
foreach (JObject data in dataArr)
{
JObject jo = JObject.Parse(data.ToString());
DataRow row = result.NewRow();
foreach (var columnName in columnNames)
{
if (jo.Property(columnName) == null)
{
data.Add(columnName, "");
row[columnName] = data[columnName].ToString();
}
else
{
row[columnName] = data[columnName].ToString();
}
}
result.Rows.Add(row);
}
return result;
}
#endregion