45 lines
1.4 KiB
C#
45 lines
1.4 KiB
C#
using System.Data;
|
|
using System.Linq;
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
namespace Kreta.BusinessLogic.Utils
|
|
{
|
|
public static class JObjectExtensions
|
|
{
|
|
public static DataTable ToDataTable(this JObject jObject)
|
|
{
|
|
var props = jObject.Properties();
|
|
DataTable table = new DataTable();
|
|
var count = props.Count();
|
|
for (int i = 0; i < count; i++)
|
|
{
|
|
JProperty prop = props.ElementAt(i);
|
|
switch (prop.Value.Type)
|
|
{
|
|
case JTokenType.Integer:
|
|
table.Columns.Add(prop.Name, typeof(int));
|
|
break;
|
|
default:
|
|
table.Columns.Add(prop.Name, typeof(string));
|
|
break;
|
|
}
|
|
}
|
|
object[] values = new object[props.Count()];
|
|
for (int i = 0; i < values.Length; i++)
|
|
{
|
|
var prop = props.ElementAt(i);
|
|
switch (prop.Value.Type)
|
|
{
|
|
case JTokenType.Integer:
|
|
values[i] = prop.Value.Value<int>();
|
|
break;
|
|
default:
|
|
values[i] = prop.Value.Value<string>();
|
|
break;
|
|
}
|
|
}
|
|
table.Rows.Add(values);
|
|
return table;
|
|
}
|
|
}
|
|
}
|