Jquery json instance code

Source: Internet
Author: User

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
}
});
}

Related Article

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.