/**/ /// <Summary>
/// Convert dataset data to JSON format
/// </Summary>
/// <Param name = "ds"> Dataset </Param>
/// <Returns> JSON string </Returns>
Public Static String Getjsonbydataset (Dataset DS)
{
If (DS = Null | DS. Tables. Count <= 0 | DS. Tables [ 0 ]. Rows. Count <= 0 )
{< br> // If the queried data is null, OK is returned: false
return " {\ " OK \ " : false} " ;
}
Stringbuilder sb = New Stringbuilder ();
SB. append ( " {\ " OK \ " : True, " );
Foreach (Datatable dt In DS. Tables)
{
SB. append ( String . Format ( " \ " {0} \ " :[ " , DT. tablename ));
Foreach (Datarow Dr In DT. Rows)
{
SB. append ( " { " );
For ( Int I = 0 ; I < Dr. Table. Columns. Count; I ++ )
{
SB. appendformat ( " \ " {0} \ " :\ " {1} \ " , " , Dr. Table. Columns [I]. columnname. Replace ( " \ "" , " \\\ "" ). Replace ( " \' " , " \\\' " ), Fieldtypefilter. objtostr (Dr [I]). Replace ( " \ "" , " \\\ "" ). Replace ( " \' " , " \\\' " ));
}
SB. Remove (sb. tostring (). lastindexof ( ' , ' ), 1 );
SB. append ( " }, " );
}
SB. Remove (sb. tostring (). lastindexof ( ' , ' ), 1 );
SB. append ( " ], " );
}
SB. Remove (sb. tostring (). lastindexof ( ' , ' ), 1 );
SB. append ( " } " );
Return SB. tostring ();
}
Corresponding jquery JSON reading Method
$. Getjson ( " Page. aspx " , {Process:'Getinfo', Idx: idx} , Function (JSON) {
If (JSON. OK = True ) {
$ ( " # Slgametype " ). Val (JSON. Table [ 0 ]. Gametype );
$ ( " # Txtselectname " ). Val (JSON. Table [ 0 ]. Selectname );
$ ( " # Txtmyname " ). Val (JSON. Table1 [ 0 ]. Myname );
$ ( " # Txtmyinfo " ). Val (JSON. Table1 [ 0 ]. Info );
}
} ); Tag: converts dataset data to JSON format.