Datatable data table to JSON

Source: Internet
Author: User

The first one is sent to the front-end and Eval is required. The array is converted to Js.

The second one is converted into real JSON data.

///   <Summary>  
/// Converts a data table into a JSON string, which can be directly converted to a two-dimensional array on the client side.
///   </Summary>  
///   <Param name = "Source"> The table to be converted. </Param>  
///   <Returns> </returns>  
Public   Static   String Datatabletojson (datatable source)
{
If (Source. Rows. Count =   0 )
Return   "" ;
Stringbuilder sb =   New Stringbuilder ( " [ " );
Foreach (Datarow row In Source. Rows)
{
SB. append ( " [ " );
For ( Int I =   0 ; I < Source. Columns. Count; I ++ )
{
SB. append ( ' " '   + Row. tostring () +   " \ " , " );
}
SB. Remove (sb. Length -   1 , 1 );
SB. append ( " ], " );
}
SB. Remove (sb. Length -   1 , 1 );
SB. append ( " ] " );
Return SB. tostring ();
}

 

///   <Summary>  
/// Returns the JSON data to the foreground.
///   </Summary>  
///   <Param name = "DT"> Data Table </Param>  
///   <Returns> JSON string </Returns>  
Public   String Createjsonparameters (datatable DT)
{
Stringbuilder jsonstring =   New Stringbuilder ();
// Exception Handling
If (DT ! =   Null   && DT. Rows. Count >   0 )
{
Jsonstring. append ( " { " );
Jsonstring. append ( " \ " Tableinfo \ " :[ " );
For ( Int I =   0 ; I < DT. Rows. Count; I ++ )
{
Jsonstring. append ( " { " );
For ( Int J =   0 ; J < DT. Columns. Count; j ++ )
{
If (J < DT. Columns. Count -   1 )
{
Jsonstring. append ( " \ "" + Dt. Columns [J]. columnname. tostring () + " \ " : "   +   " \ "" + Dt. Rows [J]. tostring () + " \ " , " );
}
Else   If (J = DT. Columns. Count -   1 )
{
Jsonstring. append ( " \ "" + Dt. Columns [J]. columnname. tostring () + " \ " : "   +   " \ "" + Dt. Rows [J]. tostring () + " \ "" );
}
}
/**/  
/* End of string */  
If (I = DT. Rows. Count -   1 )
{
Jsonstring. append ( " } " );
}
Else  
{
Jsonstring. append ( " }, " );
}
}
Jsonstring. append ( " ]} " );
Return Jsonstring. tostring ();
}
Else  
{
Return   Null ;
}
}

 

[To] http://www.cnblogs.com/liuju150/archive/2009/07/10/database-to-JSON.html

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.