How to convert json into datatable?

前端 未结 5 721
暗喜
暗喜 2020-11-27 21:17

Does anyone know how to convert a json string into DataTable from asp.net? I came to know about the Deserialize, it needs the class, I just want the datatable as returned. C

5条回答
  •  萌比男神i
    2020-11-27 21:34

    Assuming that your JSON string is a list of objects, each object will correspond to a row in the DataTable, viz:

        public DataTable DerializeDataTable()
        {
            const string json = @"[{""Name"":""AAA"",""Age"":""22"",""Job"":""PPP""},"
                               + @"{""Name"":""BBB"",""Age"":""25"",""Job"":""QQQ""},"
                               + @"{""Name"":""CCC"",""Age"":""38"",""Job"":""RRR""}]";
            var table = JsonConvert.DeserializeObject(json);
            return table;
        }
    

    This requires the Json.NET framework.

    If your JSON structure is different please post it. Best Regards.

提交回复
热议问题