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:
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Text.RegularExpressions;
- public class ConvertJsonStringToDataTable
- {
- public DataTable JsonStringToDataTable(string jsonString)
- {
- DataTable dt = new DataTable();
- string[] jsonStringArray = Regex.Split(jsonString.Replace("[", "").Replace("]", ""), "},{");
- List<string> ColumnsName = new List<string>();
- foreach (string jSA in jsonStringArray)
- {
- string[] jsonStringData = Regex.Split(jSA.Replace("{", "").Replace("}", ""), ",");
- foreach (string ColumnsNameData in jsonStringData)
- {
- try
- {
- int idx = ColumnsNameData.IndexOf(":");
- string ColumnsNameString = ColumnsNameData.Substring(0, idx - 1).Replace("\"", "");
- if (!ColumnsName.Contains(ColumnsNameString))
- {
- ColumnsName.Add(ColumnsNameString);
- }
- }
- catch (Exception ex)
- {
- throw new Exception(string.Format("Error Parsing Column Name : {0}", ColumnsNameData));
- }
- }
- break;
- }
- foreach (string AddColumnName in ColumnsName)
- {
- dt.Columns.Add(AddColumnName);
- }
- foreach (string jSA in jsonStringArray)
- {
- string[] RowData = Regex.Split(jSA.Replace("{", "").Replace("}", ""), ",");
- DataRow nr = dt.NewRow();
- foreach (string rowData in RowData)
- {
- try
- {
- int idx = rowData.IndexOf(":");
- string RowColumns = rowData.Substring(0, idx - 1).Replace("\"", "");
- string RowDataString = rowData.Substring(idx + 1).Replace("\"", "");
- nr[RowColumns] = RowDataString;
- }
- catch (Exception ex)
- {
- continue;
- }
- }
- dt.Rows.Add(nr);
- }
- return dt;
- }
- }
Step 2
Write the following design code:
- <asp:Label ID="ui_lbl_JsonString" runat="server"></asp:Label>
- <br />
- <br />
- <asp:Button ID="ui_btn_Convert1" runat="server" Text="Manually Convert into DataTable" OnClick="ui_btn_Convert1_Click" />
- <asp:Button ID="ui_btn_Reload" runat="server" Text="Reload" OnClick="ui_btn_Reload_Click" />
- <br />
- <br />
- <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:
- using System;
- using System.Data;
- using System.IO;
- public partial class _Default : System.Web.UI.Page
- {
- #region Global Variable
- DataTable dt;
- string JsonString = string.Empty;
- ConvertJsonStringToDataTable jDt = new ConvertJsonStringToDataTable();
- #endregion
- protected void Page_Load(object sender, EventArgs e)
- {
- if (!IsPostBack)
- {
- string FileName = "JSONString.txt";
- var stream = File.OpenText(Server.MapPath(FileName));
- JsonString = stream.ReadToEnd();
- ui_lbl_JsonString.Text = JsonString;
- }
- protected void ui_btn_Convert1_Click(object sender, EventArgs e)
- {
- JsonString = ui_lbl_JsonString.Text;
- dt = jDt.JsonStringToDataTable(JsonString);
- ui_grdVw_EmployeeDetail1.DataSource = dt;
- ui_grdVw_EmployeeDetail1.DataBind();
- }
- protected void ui_btn_Reload_Click(object sender, EventArgs e)
- {
- Response.Redirect(Request.RawUrl);
- }
- }
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:
- <asp:Button ID="ui_btn_Convert2" runat="server" Text="Newtonsoft Convert into DataTable" OnClick="ui_btn_Convert2_Click" />
- <br />
- <br />
- <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.
- protected void ui_btn_Convert2_Click(object sender, EventArgs e)
- {
- JsonString = ui_lbl_JsonString.Text;
- dt = (DataTable)JsonConvert.DeserializeObject(JsonString, (typeof(DataTable)));
- ui_grdVw_EmployeeDetail2.DataSource = dt;
- ui_grdVw_EmployeeDetail2.DataBind();
- }
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.