<summary>///Convert JSON to DataTable///</summary>//<param name= "Strjson" > Get JS on</param>//<returns></returns> private DataTable jsontodatatable (string Strjson) {//convert JSON format Strjson = strjson.replace (", \" "," *\ ""). Replace ("\": "," \ "#"). ToString (); Remove the table name var RG = new Regex (@ "(? <={) [^:]+ (? =:\[)", regexoptions.ignorecase); String strName = RG. Match (Strjson). Value; DataTable TB = null; Remove table name Strjson = strjson.substring (Strjson.indexof ("[") + 1); Strjson = strjson.substring (0, Strjson.indexof ("]")); Get Data RG = new Regex (@ "(? <={) [^}]+ (? =})"); MatchCollection MC = RG. Matches (Strjson); for (int i = 0; I < MC. Count; i++) {string strrow = Mc[i]. Value; string[] strrows = strrow.split (' * '); Create a table if (TB = = NULL) {TB = new DataTable (); Tb. TableName = StrName; foreach (String str in strrows) {var dc = new DataColumn (); string[] Strcell = str. Split (' # '); if (Strcell[0]. Substring (0, 1) = = "\" ") {int a = strcell[0]. Length; dc. ColumnName = strcell[0]. Substring (1, a-2); } else {DC. ColumnName = strcell[0]; } TB. Columns.Add (DC); } TB. AcceptChanges (); }//Add content to DataRow dr = TB. NewRow (); for (int r = 0; r < strrows.length; r++) {Dr[r] = Strrows[r]. Split (' # ')[1]. Trim (). Replace (",", ","). Replace (":", ":"). Replace ("\" "," "); } TB. Rows.Add (DR); Tb. AcceptChanges (); } return TB; }
Json string converted to DataTable data collection