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 ();
}
}
Copy code using Jsonutil ju = new Jsonutil ();
Response.Write (Ju. ToJson (DS));
Copy code if parsing has a problem, you can replace the "" "in the code with" \ ""
How ASP. NET converts a dataset to josn and outputs