Copy Code code as follows:
public class Jsonutil
{
public string Tojson (DataSet dataset)
{
String jsonstring = "{";
foreach (DataTable table in Dataset.tables)
{
jsonstring = "" "+ table. TableName + "" ":" + tojson (table) + ",";
}
jsonstring = Jsonstring.trimend (', ');
return jsonstring + "}";
}
public string Tojson (DataTable dt)
{
StringBuilder jsonstring = new StringBuilder ();
Jsonstring.append ("[");
DataRowCollection DRC = dt. Rows;
for (int i = 0; i < DRC. Count; i++)
{
Jsonstring.append ("{");
for (int j = 0; j < dt.) Columns.count; J + +)
{
String strkey = dt. COLUMNS[J]. ColumnName;
String strvalue = Drc[i][j]. ToString ();
Type type = dt. COLUMNS[J]. DataType;
Jsonstring.append ("" "" + Strkey + "": ");
strvalue = String.Format (strvalue, type);
Jsonstring.append ("" "" + strvalue + "" ",");
}
Jsonstring.append ("},");
}
Jsonstring.remove (jsonstring.length-1, 1);
Jsonstring.append ("]");
return jsonstring.tostring ();
}
}
Use
Copy Code code as follows:
Jsonutil ju = new Jsonutil ();
Response.Write (Ju. Tojson (DS));
If parsing has problems, you can replace the "" "" "in the code with" \ "