Convert JSON String to DataTable in ASP.Net

First of all you need to create an "ASP.NET Empty Web Site". Then use the following procedure.

Step 1

Create a ConvertJsonStringToDataTable class in the App_Code folder and provide the following:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Text.RegularExpressions;

  5. public class ConvertJsonStringToDataTable
  6. {
  7.    public DataTable JsonStringToDataTable(string jsonString)
  8.    {
  9.       DataTable dt = new DataTable();
  10.       string[] jsonStringArray = Regex.Split(jsonString.Replace("[", "").Replace("]", ""), "},{");
  11.       List<string> ColumnsName = new List<string>();
  12.       foreach (string jSA in jsonStringArray)
  13.       {
  14.          string[] jsonStringData = Regex.Split(jSA.Replace("{", "").Replace("}", ""), ",");
  15.          foreach (string ColumnsNameData in jsonStringData)
  16.          {
  17.             try
  18.             {
  19.                int idx = ColumnsNameData.IndexOf(":");
  20.                string ColumnsNameString = ColumnsNameData.Substring(0, idx - 1).Replace("\"", "");
  21.                if (!ColumnsName.Contains(ColumnsNameString))
  22.                {
  23.                   ColumnsName.Add(ColumnsNameString);
  24.                }
  25.             }
  26.             catch (Exception ex)
  27.             {
  28.                throw new Exception(string.Format("Error Parsing Column Name : {0}", ColumnsNameData));
  29.             }
  30.          }
  31.          break;
  32.       }
  33.       foreach (string AddColumnName in ColumnsName)
  34.       {
  35.          dt.Columns.Add(AddColumnName);
  36.       }
  37.       foreach (string jSA in jsonStringArray)
  38.       {
  39.          string[] RowData = Regex.Split(jSA.Replace("{", "").Replace("}", ""), ",");
  40.          DataRow nr = dt.NewRow();
  41.          foreach (string rowData in RowData)
  42.          {
  43.             try
  44.             {
  45.                int idx = rowData.IndexOf(":");
  46.                string RowColumns = rowData.Substring(0, idx - 1).Replace("\"", "");
  47.                string RowDataString = rowData.Substring(idx + 1).Replace("\"", "");
  48.                nr[RowColumns] = RowDataString;
  49.             }
  50.             catch (Exception ex)
  51.             {
  52.                continue;
  53.             }
  54.          }
  55.          dt.Rows.Add(nr);
  56.       }
  57.       return dt;
  58.    }
  59. }

Step 2

Write the following design code:

  1. <asp:Label ID="ui_lbl_JsonString" runat="server"></asp:Label>
  2. <br />
  3. <br />
  4. <asp:Button ID="ui_btn_Convert1" runat="server" Text="Manually Convert into DataTable" OnClick="ui_btn_Convert1_Click" />
  5. <asp:Button ID="ui_btn_Reload" runat="server" Text="Reload" OnClick="ui_btn_Reload_Click" />
  6. <br />
  7. <br />
  8. <asp:GridView ID="ui_grdVw_EmployeeDetail1" runat="server" Width="50%" HeaderStyle-CssClass="pageheading"></asp:GridView>

Step 3

Now, open the Deafult.asps.cs then write the following code:

  1. using System;
  2. using System.Data;
  3. using System.IO;

  4. public partial class _Default : System.Web.UI.Page
  5. {
  6.    #region Global Variable
  7.    DataTable dt;
  8.    string JsonString = string.Empty;
  9.    ConvertJsonStringToDataTable jDt = new ConvertJsonStringToDataTable();
  10.    #endregion

  11. protected void Page_Load(object sender, EventArgs e)
  12. {
  13.    if (!IsPostBack)
  14.    {
  15.       string FileName = "JSONString.txt";
  16.       var stream = File.OpenText(Server.MapPath(FileName));
  17.       JsonString = stream.ReadToEnd();
  18.       ui_lbl_JsonString.Text = JsonString;
  19.    }

  20.    protected void ui_btn_Convert1_Click(object sender, EventArgs e)
  21.    {
  22.       JsonString = ui_lbl_JsonString.Text;
  23.       dt = jDt.JsonStringToDataTable(JsonString);   
  24.       ui_grdVw_EmployeeDetail1.DataSource = dt;
  25.       ui_grdVw_EmployeeDetail1.DataBind();
  26.    }

  27.    protected void ui_btn_Reload_Click(object sender, EventArgs e)
  28.    {
  29.       Response.Redirect(Request.RawUrl);
  30.    }
  31. }

Step 4

Press F5, run the 

Now, convert the JSON string to DataTable using the newtonsoft DLL.

Step 1

Download the Newtonsoft DLL and move it to the ASP.Net project's bin folder.

Step 2

Then, Open the Deafult.aspx page and write the following design code:

  1. <asp:Button ID="ui_btn_Convert2" runat="server" Text="Newtonsoft Convert into DataTable" OnClick="ui_btn_Convert2_Click" />
  2. <br />
  3. <br />
  4. <asp:GridView ID="ui_grdVw_EmployeeDetail2" runat="server" Width="50%" HeaderStyle-CssClass="pageheading"></asp:GridView> 

Step 3

Now, write the following code in Default.aspx.cs:

using this namespace.

  1. using Newtonsoft.Json;
 And then:
  1. protected void ui_btn_Convert2_Click(object sender, EventArgs e)
  2. {
  3.    JsonString = ui_lbl_JsonString.Text;
  4.    dt = (DataTable)JsonConvert.DeserializeObject(JsonString, (typeof(DataTable)));
  5.    ui_grdVw_EmployeeDetail2.DataSource = dt;
  6.    ui_grdVw_EmployeeDetail2.DataBind();
  7. }
Run the project and click on the Newtonsoft Convert to DataTable.
 

In the preceding code, I convert the JSON String to DataTable.

I have attached the sample project for this, download and see how it works.

Thank you.

Ebook Download
View all
Learn
View all