How ASP. NET converts a dataset to josn and outputs

Source: Internet
Author: User
Tags tojson

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.