Convert datatable to JSON format

Source: Internet
Author: User

Convert datatable to JSON format: datatableconvertjson. CS

Public class datatableconvertjson
{

# Region datatable converted to JSON format
/// <Summary>
/// Datatable converted to JSON format
/// </Summary>
/// <Param name = "DT"> </param>
/// <Returns> </returns>
Public static string datatable2json (datatable DT)
{
Stringbuilder jsonbuilder = new stringbuilder ();
Jsonbuilder. append ("{\"");
Jsonbuilder. append (Dt. tablename );
Jsonbuilder. append ("\":[");
Jsonbuilder. append ("[");
For (INT I = 0; I <DT. Rows. Count; I ++)
{
Jsonbuilder. append ("{");
For (Int J = 0; j <DT. Columns. Count; j ++)
{
Jsonbuilder. append ("\"");
Jsonbuilder. append (Dt. Columns [J]. columnname );
Jsonbuilder. append ("\":\"");
Jsonbuilder. append (Dt. Rows [I] [J]. tostring ());
Jsonbuilder. append ("\",");
}
Jsonbuilder. Remove (jsonbuilder. Length-1, 1 );
Jsonbuilder. append ("},");
}
Jsonbuilder. Remove (jsonbuilder. Length-1, 1 );
Jsonbuilder. append ("]");
Jsonbuilder. append ("}");
Return jsonbuilder. tostring ();
}

# Convert endregion datatable to JSON format
# Region dataset converted to JSON format
/// <Summary>
/// Convert dataset to JSON format
/// </Summary>
/// <Param name = "ds"> dataset </param>
/// <Returns> </returns>
Public static string dataset2json (Dataset DS)
{
Stringbuilder JSON = new stringbuilder ();

Foreach (datatable DT in DS. Tables)
{
JSON. append ("{\"");
JSON. append (Dt. tablename );
JSON. append ("\":");
JSON. append (datatable2json (DT ));
JSON. append ("}");
} Return JSON. tostring ();
}
# Endregion

/// <Summary>
/// Msdn
/// </Summary>
/// <Param name = "jsonname"> </param>
/// <Param name = "DT"> </param>
/// <Returns> </returns>
Public static string datatabletojson (string jsonname, datatable DT)
{
Stringbuilder JSON = new 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-1)
{
JSON. append (",");
}
}
JSON. append ("}");
If (I <DT. Rows. Count-1)
{
JSON. append (",");
}
}
}
JSON. append ("]}");
Return JSON. tostring ();
}
}

Call JSON = datatableconvertjson. datatabletojson ("tble", tbldatas );

// Quotation_no is a column of tbldatas.
Alert (msg. tble [I]. quotation_no)

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.