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(); break; default: values[i] = prop.Value.Value(); break; } } table.Rows.Add(values); return table; } } }