其他分享
首页 > 其他分享> > Json转DataTable

Json转DataTable

作者:互联网

public static DataTable JsonToDataTable(string json)
{
    DataTable table = new DataTable();
    //JsonStr为Json字符串
    JArray array = JsonConvert.DeserializeObject(json) as JArray;//反序列化为数组
    if (array.Count > 0)
    {
        StringBuilder columns = new StringBuilder();
        
        JObject objColumns = array[0] as JObject;
        //构造表头
        foreach (JToken jkon in objColumns.AsEnumerable<JToken>())
        {
            string name = ((JProperty)(jkon)).Name;
            columns.Append(name + ",");
            table.Columns.Add(name);
        }
        //向表中添加数据
        for (int i = 0; i < array.Count; i++)
        {
            DataRow row = table.NewRow();
            JObject obj = array[i] as JObject;
            foreach (JToken jkon in obj.AsEnumerable<JToken>())
            {
                
                string name = ((JProperty)(jkon)).Name;
                string value = ((JProperty)(jkon)).Value.ToString();
                row[name] = value;
            }
            table.Rows.Add(row);
        }
    }
    return table;
}

标签:name,JObject,DataTable,Json,table,jkon,array,string
来源: https://www.cnblogs.com/Small-whites/p/16531270.html