Ajax解析josn

A.js

//查询一行数据

              $("#Btsouso").bind("click", function () {
                  var searchid = $("#txtids").val();
                  $.ajax({
                      type: "post",
                      dataType: "json",
                      url: "Mnet.ashx",
                      cache: false,
                      //async: false,
                      data: { operate: "getuserdetails", sid: searchid },
                      success: function (text, textStatus) {
                          if (text != null) {
                              $("#results").html("用户名 :" + text.userName + " + 姓名 :" +

text.name + " 地址 :" + text.place + " ");
                          }
                      },
                      complete: function (XMLHttpRequest, textStatus) {
                      },
                      error: function (XMLHttpRequest, textStatus, errorThrown) {
                          //alert(XMLHttpRequest.responseText);
                      }
                  });
              });


              //查询多行数据
              $("#btgetmoney").bind("click", function () {
                  var searchid = $("#txtids").val();
                  $.ajax({
                      type: "post",
                      dataType: "json",
                      url: "Mnet.ashx",
                      cache: false,
                      //async: false,
                      data: { operate: "getManyUser", sid: searchid },
                      success: function (text, textStatus) {
                          if (text != null) {
                              $.each(text, function (i, item) {
                                  $("#results").append("用户名 :" + item.yhusername + " + 姓名 :

" + item.yhname + " 地址 :" + item.dizhi + "<br> ");
                              });
                          }
                      },
                      complete: function (XMLHttpRequest, textStatus) {
                      },
                      error: function (XMLHttpRequest, textStatus, errorThrown) {
                       
                      }
                  });

              });


ashx:

/// <summary>
    ///
    /// </summary>
    private void getuserdetails(HttpContext context)
    {
        var id = Convert.ToInt32(context.Request["sid"]);
        CodeModel.BLL.userinfo yhbll = new CodeModel.BLL.userinfo();
        CodeModel.Model.userinfo yhModel = new CodeModel.Model.userinfo();
        yhModel=yhbll.GetModel(int.Parse(id.ToString()));
        context.Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(yhModel));
    
    }

    /// <summary>
    ///
    /// </summary>
    private void getMoneyusers(HttpContext context)
    {
        var id = context.Request["sid"].ToString();
      CodeModel.BLL.userinfo yhbll = new CodeModel.BLL.userinfo();
        CodeModel.Model.userinfo yhModel = new CodeModel.Model.userinfo();
        IList<CodeModel.Model.userinfo> list = yhbll.GetUserList("type='"+id+"'");
        if (list.Count > 0)
        {
            var Json = Newtonsoft.Json.JsonConvert.SerializeObject(list.Select(
                               t => new
                               {
                                   yhid = t.id,
                                   yhname = t.name,
                                   yhusername=t.userName,
                                   dizhi = t.place,
                                
                               }));
            context.Response.Write(Json);
        }
    }
    C.SQL


        //DataSet转换成List<ProductTypeInfo>

        public IList <CodeModel.Model.userinfo> GetUserList(string strwhere)
        {
            IList<CodeModel.Model.userinfo> yhlist = new List<Model.userinfo>();
            CodeModel.BLL.userinfo yh = new BLL.userinfo();
           
            //等待转换的DataSet
            DataSet ds = yh.GetList(strwhere);
              //遍历ds中Tables[0]数据
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)//逐行遍历  i 是当前行
            {
                CodeModel.Model.userinfo yhmodel = new Model.userinfo();
                yhmodel.id = Convert.ToInt32(ds.Tables[0].Rows[i]["id"]);
                yhmodel.userName = ds.Tables[0].Rows[i]["userName"].ToString();
                yhmodel.name = ds.Tables[0].Rows[i]["name"].ToString();
                yhmodel.place = ds.Tables[0].Rows[i]["place"].ToString();
                //将对象插入到List中
                yhlist.Add(yhmodel);
            }
             //返回List
            return yhlist;
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值