.netdatatablejson.netdeserialization

How to convert JSON into a datatable?


Does anyone know how to convert a JSON string into a DataTable in ASP.NET? I know about Deserializing; I just want the datatable returned. Can anyone tell me how to convert it to a 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 Newtonsoft's Json.NET framework.