public DataSet ConvertJsonToDatatable(string jsonString)
{
DataSet ds = new DataSet();
DataTable dt = new DataTable("Dash");
ds.Tables.Add(dt);
string s = "{" + "\"" + "RetDMDashCaption" + "\"" + ":";
jsonString = jsonString.Replace(s, "").Trim('}');
string[] jsonParts = Regex.Split(jsonString.Replace("[", "").Replace("]", ""), "},{");
List<string> dtColumns = new List<string>();
foreach (string jp in jsonParts)
{
string[] propData = Regex.Split(jp.Replace("{", "").Replace("}", ""), ",");
foreach (string rowData in propData)
{
try
{
int idx = rowData.IndexOf(":");
string n = rowData.Substring(0, idx - 1);
string v = rowData.Substring(idx + 1);
if (!dtColumns.Contains(n))
{
dtColumns.Add(n.Replace("\"", ""));
}
}
catch (Exception ex)
{
throw new Exception(string.Format("Error Parsing Column Name : {0}", rowData));
}
}
break;
}
foreach (string c in dtColumns)
{
dt.Columns.Add(c);
}
foreach (string jp in jsonParts)
{
string[] propData = Regex.Split(jp.Replace("{", "").Replace("}", ""), ",");
DataRow nr = dt.NewRow();
foreach (string rowData in propData)
{
try
{
int idx = rowData.IndexOf(":");
string n = rowData.Substring(0, idx - 1).Replace("\"", "");
string v = rowData.Substring(idx + 1).Replace("\"", "");
nr[n] = v;
}
catch (Exception ex)
{
continue;
}
}
dt.Rows.Add(nr);
}
return ds;
}
// Method to Convert DataTable to JSON String (Key Stored in Physical File).
public List<string> ConvertTableToList(DataTable dt)
{
List<string> records = new List<string>();
records.Clear();
string rowdata;
string enc;
foreach (DataRow row in dt.Rows)
{
rowdata = null;
enc = null;
rowdata = (new JavaScriptSerializer() { MaxJsonLength = 2147483644 }).Serialize(ConvertTable(row));
enc = Encrypt(rowdata);
records.Add(enc);
}
return records;
}
// Make the object of the Class and set the values of its Properties.
private Record ConvertTable(DataRow row)
{
Record rec = new Record();
rec.mcode = Convert.ToInt32(row["mcode"].ToString());
rec.state_code = Convert.ToInt32(row["state_code"].ToString());
rec.district_code = Convert.ToInt32(row["district_code"].ToString());
rec.teh_code = Convert.ToInt32(row["teh_code"].ToString());
rec.blk_code = Convert.ToInt32(row["blk_code"].ToString());
rec.gp_code = Convert.ToInt32(row["gp_code"].ToString());
rec.vill_code = Convert.ToInt32(row["vill_code"].ToString());
rec.sector_code = Convert.ToInt32(row["sector_code"].ToString());
rec.dept_code = Convert.ToInt32(row["dept_code"].ToString());
rec.project_code = Convert.ToInt32(row["project_code"].ToString());
rec.yr = Convert.ToInt32(row["yr"].ToString());
rec.mnth = Convert.ToInt32(row["mnth"].ToString());
rec.dataportmode = Convert.ToInt32(row["dataportmode"].ToString());
rec.modedesc = row["modedesc"].ToString();
rec.data_lvl_code = Convert.ToInt32(row["data_lvl_code"].ToString());
rec.cnt1 = Convert.ToDecimal(row["cnt1"].ToString());
rec.cnt2 = Convert.ToDecimal(row["cnt2"].ToString());
rec.cnt3 = Convert.ToDecimal(row["cnt3"].ToString());
rec.cnt4 = Convert.ToDecimal(row["cnt4"].ToString());
rec.cnt5 = Convert.ToDecimal(row["cnt5"].ToString());
return rec;
}
No comments:
Post a Comment