- 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;
- }
- }
Wednesday 20 September 2017
Convert JSON String to DataTable in ASP.Net
Subscribe to:
Post Comments (Atom)
Upload valid file in C#
protected bool CheckFileExtandLength(HttpPostedFile HtmlDocFile) { try { Dictionary<string, byte[]>...
-
CREATE TAblE #temp ( T_Name VARCHAR(50), T_Times BIGINT ) INSERT INTO #temp(T_Name,T_Times) VALUES ('ASHISH',4) IN...
-
Step 1: Open URL: https://portal.azure.com/#home Step2 : Click Manage Azure Active Director Step3: Click Enterprise Application Step 4: Ne...
-
Injection Injection flaws, such as SQL injection, LDAP injection, and CRLF injection, occur when an attacker sends untrusted data to an ...
No comments:
Post a Comment