jquery json简单例子

       $(document).ready(function(){
          
           //取数据库数据
            $.ajax(
             {
                type:"get",
               dataType:"json",
               url:"AjaxHandler.ashx",
               data:"id=1",
               success:function(msg)
               {
                  var data=msg.Table;
                  $.each(data,function(i,n)
                               {                                                                                         
                                  var row=$("#temp").clone();
                                  row.find("#listtile").text(n.blog_id);
                                  row.find("#listvalue").text(n.blog_name);
                                  row.attr("id","ready");                                
                                  row.appendTo("#mainbox");
                               }
                  );
              
               }
             }
           );
});

处理端:
using System;
using System.Data;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Data.SqlClient;
using System.Text;
using WebSite.ADOFramework;

namespace Ajax
{
    /// <summary>
    /// $codebehindclassname$ 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class ajxreponse : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
         

            DataTable dt = SqlHelper.ExecuteDataset(SqlHelper.CONN_STRING_NON_DTC, CommandType.Text, "SELECT * FROM Blogs ").Tables[0];
            dt.WriteXml("c:\\aaa.xml");
            context.Response.Write(DtToSON(dt));

        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
        public  string DtToSON(DataTable dt)
        {
            StringBuilder jsonBuilder = new StringBuilder();
            jsonBuilder.Append("{\"");
            jsonBuilder.Append(dt.TableName.ToString());
            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("}");
            return jsonBuilder.ToString();
        }

    }
}

posted @ 2008-08-12 22:09  hemingchen  阅读(945)  评论(0编辑  收藏  举报