jquery json

  1. 网上转载 
  2. 页面代码
  3. <%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="MyTestWebApp.JsonData.Default" %>  
  4. <asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">  
  5.     <script src="../Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>  
  6.     <script type="text/javascript">  
  7.         <pre name="code" class="javascript">$(document).ready(function () {  
  8.             $.ajax({  
  9.                 type: "get",  
  10.                 dataType: "Json",  
  11.                 url: "JsonHandler.ashx",  
  12.                 start: function () { alert("开始获取数据了") },  
  13.                 complete: function () { alert("获取完了") },  
  14.                 success: function (data) {  
  15.                     var t = eval(data); //强制转换一下json字符串,生成json对象  
  16.                     $.each(t, function (i, n) {  
  17.                         var row = $("#template").clone(); //克隆模板,创建一个新数据行  
  18.                         for (attribute in n) {  
  19.                             row.find("#" + attribute).html(n[attribute]); //循环json对象的属性,并赋值到数据行中对应的列,此处列的id就是相应的属性名称  
  20.                         }  
  21.                         row.appendTo($("#testTable"));  
  22.                     });  
  23.                 }  
  24.             });  
  25.         });  

</script></asp:Content><asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"><table id="testTable"> <th>编号</th><th>标题</th><th>内容</th> <!--数据模板--> <!--其中每一列的id就是对应记录中的列名--> <tr id="template"><td id="Id"></td><td id="title"></td><td id="content"></td></tr> <!--数据模板--></table></asp:Content> 

一般处理页面代码:

    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Linq;  
    4. using System.Web;  
    5. using MyTestWebApp.Code;  
    6. using System.Data;  
    7.   
    8. namespace MyTestWebApp.JsonData  
    9. {  
    10.     /// <summary>   
    11.     /// JsonHandler 的摘要说明   
    12.     /// </summary>   
    13.     public class JsonHandler : IHttpHandler  
    14.     {  
    15.   
    16.         public void ProcessRequest(HttpContext context)  
    17.         {  
    18.             context.Response.ContentType = "text/javascript";  
    19.             DataTable table = SqlHelper.ExecuteDataset(SqlHelper.connectionString, CommandType.Text, "select Id, title, content from Accordion").Tables[0];  
    20.             context.Response.Write(JSONHelper.DataTableToJSON(table));  
    21.         }  
    22.   
    23.         public bool IsReusable  
    24.         {  
    25.             get  
    26.             {  
    27.                 return false;  
    28.             }  
    29.         }  
    30.     }  
    31. }  
原文地址:https://www.cnblogs.com/Yellowshorts/p/2877514.html