Copy codeThe Code is as follows:
1. Get DataSet
2. Convert to DataTable
3.
Public static string DataTableToJson (DataTable dt)
{
StringBuilder jsonBuilder = new StringBuilder ();
// JsonBuilder. Append ("{"); // output requirements do not require '}'
// JsonBuilder. Append (dt. TableName );
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 ("}"); // output requirements do not require '}'
Return jsonBuilder. ToString ();
}
4. Output The Json string Response.
5. Obtain the Json string
Function load () // page loading
{
$. Ajax ({
Type: "post ",
Url: "../Handler/LinkManHandler. ashx", // where to obtain the Json
Data: "action = getLinkMan & id =" + comId, // parameters passed by Ajax
BeforeSend: function (){
// Display loading or loading before obtaining
},
Success: function (mess)
{
$ ("# DivImage"). hide ();
If (mess! = "]") // If the obtained data is not empty
{
Var type = eval (mess );
Var str = "<a> homepage </a>"
For (var I = 0; I <type. length; I ++)
{
// Operation type
Str + = "<a>" + type [I]. database field name + "</a>
}
$ ("# DivLinkMan" 2.16.html (str); // embed str into div divLinkMan
}
Else
{
Var notfind = "$ ("# DivLinkMan" ).html (notfind );
}
},
Complete: function ()
{
// All methods after execution
}
});
}