/// <summary> ///convert JSON to DataTable/// </summary> /// <param name= "Strjson" >the resulting JSON</param> /// <returns></returns> PrivateDataTable jsontodatatable (stringStrjson) { //Convert JSON formatStrjson = Strjson.replace (",\"","*\""). Replace ("\":","\"#"). ToString (); //Remove Table name varRG =NewRegex (@"(? <={) [^:]+ (? =:\[)", regexoptions.ignorecase); stringStrName =RG. Match (Strjson). Value; DataTable TB=NULL; //Remove Table nameStrjson = strjson.substring (Strjson.indexof ("[") +1); Strjson= Strjson.substring (0, Strjson.indexof ("]")); //Get DataRG =NewRegex (@"(? <={) [^}]+ (? =})"); MatchCollection MC=RG. Matches (Strjson); for(inti =0; I < MC. Count; i++) { stringStrrow =Mc[i]. Value; string[] strrows = Strrow.split ('*'); //Create a table if(TB = =NULL) {TB=NewDataTable (); Tb. TableName=StrName; foreach(stringStrinchstrrows) { varDC =NewDataColumn (); string[] Strcell = str. Split ('#'); if(strcell[0]. Substring (0,1) =="\"") { intA = strcell[0]. Length; dc. ColumnName= strcell[0]. Substring (1A2); } Else{DC. ColumnName= strcell[0]; } TB. Columns.Add (DC); } TB. AcceptChanges (); } //Add ContentDataRow dr =TB. NewRow (); for(intR =0; R < Strrows.length; r++) {Dr[r]= Strrows[r]. Split ('#')[1]. Trim (). Replace (",",","). Replace (":",":"). Replace ("\"",""); } TB. Rows.Add (DR); Tb. AcceptChanges (); } returnTB; }
Note: The JSON format should be standardized
[{age:22,height:170}]
For this format is not judged, if this is not translated correctly, to give price double quotation marks
[{"Age": $, "height": 170}]
JSON to DataTable (method of regular expressions)