asp.net输出json

今天在项目中要使用到jquery来操作json,首先通过jquery的ajax来请求asp.net输出数据json格式,之后jquery来 操作json数据。

首先看下asp.net的文件CustomerEdit_sale.aspx后台代码:
Code [
        protected void Page_Load(object sender, EventArgs e)
        {
                if (!Page.IsPostBack)
                {
                    string action = Utils.Request("action");
                    if (action == "getcustomerbasic") //ajax请求客户基本信息
                    {
                        this.customerBasic(Utils.Request("wd"));
                        Utils.End();
                    }
                  }
    }

    //输出json
        private void customerBasic(string strCustID)
        {
            Response.ContentType = "text/plain";
            string strJson = string.Empty;
            TelSales telSale = new TelSales();
            DataSet dsCust = telSale.QueryCustomers("customer_id="   strCustID);
            strJson = Utils.DataTableToJson("info", dsCust.Tables[0]);

            Response.Write(strJson);
        }

以上代码asp.net把datatable转为json的代码在这篇文章的DataTableToJson方法:

   //DataTable转成Json
        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();
        }

        //List转成json
        public static string ObjectToJson(string jsonName, IList IL)
        {
            StringBuilder Json = new StringBuilder();
            Json.Append("{/"" + jsonName + "/":[");
            if (IL.Count > 0)
            {
                for (int i = 0; i < IL.Count; i++)
                {
                    T obj = Activator.CreateInstance();
                    Type type = obj.GetType();
                    PropertyInfo[] pis = type.GetProperties();
                    Json.Append("{");
                    for (int j = 0; j < pis.Length; j++)
                    {
                        Json.Append("/"" + pis[j].Name.ToString() + "/":/"" + pis[j].GetValue(IL[i], null) + "/"");
                        if (j < pis.Length - 1)
                        {
                            Json.Append(",");
                        }
                    }
                    Json.Append("}");
                    if (i < IL.Count - 1)
                    {
                        Json.Append(",");
                    }
                }
            }
            Json.Append("]}");
            return Json.ToString();
        }

 

 

前台调用输出的内容为:

{"info":[{"customer_id":"1883294","customer_name":"","last_incoming_date":"2009-10-19 15:49:00","customer_sex":"1","salesman":"6a66f4d0-720f-4e10-8161- fadd7a3464cc","customer_type":"D","province":"湖南省","city":"长沙 市","stock_money":"5","media_id":"189","birthday":"1900-1-1 0:00:00","saleman":"","is_filing":"True","is_new":"True","customer_business":" 未知","customer_age":"未知","remark":"","customer_IdCard":"","stock_age":"未 知","mail_box":"","post_code":"","product_id":"","address":"","stock_software":"","source_level":"","max_source_level":"","media_name":"","advertising_name":""}]}

好,下面写一个前台的jquery代码:

你可能感兴趣的:(c#)