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