Search code examples
.netdatatablejson.netdeserialization

How to convert json into datatable?


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. Can anyone tell me how to convert it to datatable?


Solution

  • 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<DataTable>(json);
            return table;
        }
    

    This requires the Json.NET framework.

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