The project uses the Jqgrid front-end data control, the data format requirements for JSON although the system has to do the corresponding implementation, but not to the DataTable to JSON has a corresponding method, so in the project to the DataTable write a go JSON extension method, see clearly is the extension method, remember S Tatic the use of the detailed extension method, please do your own Baidu.
The specific code is as follows:
Public static string datatabletojson ( this DataTable dt,string Jsonname) { system.text.stringbuilder json = new system.text.stringbuilder (); Json.append ("{\" " + jsonName + " \ ": ["); if (dt. rows.count > 0) { for (Int i = 0; i < dt. rows.count; i++) { json.append ("{"); for (int j = 0; j ≪ dt. columns.count; j++) { json.append ("\" " + dt. COLUMNS[J]. Columnname.tostring () + "\": \ "" + dt. ROWS[I][J]. ToString () + "\" "); if (J < dt. COLUMNS.COUNT&NBSP;-&NBSP;1) { json.append (","); } } json.append ("}"); if (I < dt. ROWS.COUNT&NBSP;-&NBSP;1) { json.append (","); } } } json.append ("]}"); return json.tostring (); }
C # DataTable to Json implementation