【jquery】通过【ajax】获取数据并转换成【Json】

页面代码:

<head runat="server">
    <title></title>
    <script src="jquery-1.4.2.min.js" type="text/javascript"></script>
    <script>
        function Datas() {
//            var cardName = $("#zhanghu_name").val();
//            var cardCreatedOn = $("#startDt").val();
            $.ajax({

                type: "post", //提交方式 
                cache: false,
                url: "Handler1.ashx",         //提交地址
                dataType: "json",
                // data: "name=" + cardName + "&date=" + cardCreatedOn, //提交的数据
                //data: "name=伊牛娃&date=2009-05-20",//如果只需要传固定值的话,可以直接这样写
                success: function (msg) {//根据返回状态,给出相关提示
               
                    $("#TextBox1").val(msg.tble[0].Quotation_No);
                    for (var i = 0; i < msg.tble.length; i++) {
                        alert(msg.tble[i].Quotation_No)
                    }
                     
                }
            });
        }
        
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:Button ID="Button1"   OnClientClick="Datas();return false;" runat="server" Text="Button" />
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    </div>
    </form>
</body>
</html>

处理页面:Handler1.ashx
public class Handler1 : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string json = "";
            DataTable tblDatas = new DataTable("Datas");
            tblDatas.Columns.Add("Quotation_No", Type.GetType("System.String"));
            tblDatas.Columns.Add("Cust_PN", Type.GetType("System.String"));
            tblDatas.Columns.Add("Factory_No", Type.GetType("System.String"));
            tblDatas.Rows.Add(new object[] { "Q11053 ", "BB96168-1A04", "4100" });
            tblDatas.Rows.Add(new object[] { "Q110531001", "BB96168-1A04", "4100" });
            json=  DataTableConvertJson.DataTableToJson("tble", tblDatas);

            context.Response.Write(json);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }

转换成 json格式:DataTableConvertJson.cs
public class DataTableConvertJson
    {

        #region dataTable转换成Json格式
        /// <summary>  
        /// dataTable转换成Json格式  
        /// </summary>  
        /// <param name="dt"></param>  
        /// <returns></returns>  
        public static string DataTable2Json(DataTable dt)
        {
            StringBuilder jsonBuilder = new StringBuilder();
            jsonBuilder.Append("{\"");
            jsonBuilder.Append(dt.TableName);
            jsonBuilder.Append("\":[");
            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();
        }

        #endregion dataTable转换成Json格式
        #region DataSet转换成Json格式
        /// <summary>  
        /// DataSet转换成Json格式  
        /// </summary>  
        /// <param name="ds">DataSet</param> 
        /// <returns></returns>  
        public static string Dataset2Json(DataSet ds)
        {
            StringBuilder json = new StringBuilder();

            foreach (DataTable dt in ds.Tables)
            {
                json.Append("{\"");
                json.Append(dt.TableName);
                json.Append("\":");
                json.Append(DataTable2Json(dt));
                json.Append("}");
            } return json.ToString();
        }
        #endregion

        /// <summary>
        /// Msdn
        /// </summary>
        /// <param name="jsonName"></param>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static string DataTableToJson(string jsonName, DataTable dt)
        {
            StringBuilder Json = new StringBuilder();
            Json.Append("{\"" + jsonName + "\":[");
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    Json.Append("{");
                    for (int j = 0; j < dt.Columns.Count; j++)
                    {
                        Json.Append("\"" + dt.Columns[j].ColumnName.ToString() + "\":\"" + dt.Rows[i][j].ToString() + "\"");
                        if (j < dt.Columns.Count - 1)
                        {
                            Json.Append(",");
                        }
                    }
                    Json.Append("}");
                    if (i < dt.Rows.Count - 1)
                    {
                        Json.Append(",");
                    }
                }
            }
            Json.Append("]}");
            return Json.ToString();
        }
    }


你可能感兴趣的:(jquery,json,Ajax,String,textbox,dataset)