SoFunction
Updated on 2025-03-06

C# method to parse Json into DateTable

#region parses Json into DateTable ///
/// parse Json into a DateTable.
/// Json data format is as follows: ///
 {table:[{column1:1,column2:2,column3:3},{column1:1,column2:2,column3:3}]}
/// 
///The Json string to be parsed
/// Return DateTable public DataTable JsonToDataTable(string strJson)
{
//
Take out the table name var rg = new Regex(@(?<={)[^:]+(?=:[), );
 string strName = (strJson).Value; DataTable tb = null;
// Remove the table name strJson = (([) + 1);
 strJson = (0, (]));
// Get data
 rg = new Regex(@(?<={)[^}]+(?=})); MatchCollection mc = (strJson);
 for (int i = 0; i < ; i++) {
string strRow = mc[i].Value; string[] strRows = (',');
// Create table if (tb == null) { tb = new DataTable();
  = strName; foreach (string str in strRows)
{ var dc = new DataColumn();
string[] strCell = (':');
= strCell[0].Replace(, );
 (dc); }
 ();
} // Add content DataRow dr = ();
for (int j = 0; j < ; j++)
{ dr[j] = strRows[j].Split(':')[1].Replace(,
);
} (dr);
();
 }
return tb; }
 #endregion