美文网首页
C# 拼接json字符串以及ajax遍历二维json

C# 拼接json字符串以及ajax遍历二维json

作者: 醉酒的姑娘 | 来源:发表于2018-05-29 11:28 被阅读0次

    Handler1.ashx

         public void ProcessRequest(HttpContext context)
         {
    
            context.Response.ClearContent();
            context.Response.ContentType = "text/plain";
            context.Response.Cache.SetCacheability(HttpCacheability.NoCache); //无缓存
    
            string UserID = context.Request.Params["UserID"];
            string Type = context.Request.Params["Type"];
            if (!string.IsNullOrEmpty(UserID)&&!string.IsNullOrEmpty(Type)) {
    
                switch (Type) {
                    case "Select_lxl":
                        context.Response.Write(select_lxl(UserID));
                        break;
                }
            }
         }
    
      private string select_lxl(string UserID) {
            StringBuilder JSONstring = new StringBuilder();
            JSONstring.Append("[");//这里是开始位置
            JSONstring.Append("{");
    
            //已报名
            DataSet MyDataSet3 = xh.GetSetByUser(UserID);
            if (MyDataSet3 != null && MyDataSet3.Tables[0] != null && MyDataSet3.Tables[0].Rows.Count > 0)
            {
    
                DataTable tb = MyDataSet3.Tables[0];
                JSONstring.Append("\"yx\"");
                JSONstring.Append(":");
                JSONstring.Append("[");
                for (int i = 0; i < tb.Rows.Count; i++)
                {
    
                    JSONstring.Append("{");
                    JSONstring.Append("\"associationName\":\"" + tb.Rows[i]["associationName"] + "\",");
                    JSONstring.Append("\"type\":\"" + tb.Rows[i]["type"].ToString() + "\",");
                    JSONstring.Append("\"state\":\"" + tb.Rows[i]["state"].ToString() + "\",");
                    JSONstring.Append("\"Signupid\":\"" + tb.Rows[i]["Signupid"].ToString() + "\"");
                    JSONstring.Append("},");
    
    
                }
    
                JSONstring = new StringBuilder(JSONstring.ToString().TrimEnd(','));
    
                JSONstring.Append("],");
    
    
            }
    
    
    
            //可选兴趣协会
            DataSet MyDataSet = xh.GetSetByUserAndType(UserID, "1");
            if (MyDataSet != null && MyDataSet.Tables[0] != null && MyDataSet.Tables[0].Rows.Count > 0)
            {
    
                DataTable tb = MyDataSet.Tables[0];
                JSONstring.Append("\"兴趣协会\"");
                JSONstring.Append(":");
                JSONstring.Append("[");
                for (int i = 0; i < tb.Rows.Count; i++)
                {
    
                    JSONstring.Append("{");
                    JSONstring.Append("\"pic\":\"" + tb.Rows[i]["pic"] + "\",");
                    JSONstring.Append("\"associationID\":\"" + tb.Rows[i]["associationID"].ToString() + "\",");
                    JSONstring.Append("\"associationName\":\"" + tb.Rows[i]["associationName"].ToString() + "\"");
                    JSONstring.Append("},");
    
    
                }
    
                JSONstring = new StringBuilder(JSONstring.ToString().TrimEnd(','));
                JSONstring.Append("],");
    
    
            }
    
    
    
    
            //可选兴趣协会
            DataSet MyDataSet2 = xh.GetSetByUserAndType(UserID, "2");
            if (MyDataSet2 != null && MyDataSet2.Tables[0] != null && MyDataSet2.Tables[0].Rows.Count > 0)
            {
    
                DataTable tb = MyDataSet2.Tables[0];
                JSONstring.Append("\"传统项目\"");
                JSONstring.Append(":");
                JSONstring.Append("[");
                for (int i = 0; i < tb.Rows.Count; i++)
                {
    
                    JSONstring.Append("{");
                    JSONstring.Append("\"pic\":\"" + tb.Rows[i]["pic"] + "\",");
                    JSONstring.Append("\"associationID\":\"" + tb.Rows[i]["associationID"].ToString() + "\",");
                    JSONstring.Append("\"associationName\":\"" + tb.Rows[i]["associationName"].ToString() + "\"");
                    JSONstring.Append("},");
    
    
                }
    
                JSONstring = new StringBuilder(JSONstring.ToString().TrimEnd(','));
                JSONstring.Append("]");
    
    
            }
    
            if (JSONstring.ToString().EndsWith(",")) { JSONstring = new StringBuilder(JSONstring.ToString().TrimEnd(',')); }
    
    
            JSONstring.Append("}");
            JSONstring.Append("]");
            return JSONstring.ToString();
        }
    

    jquery

        $.ajax({
        url: "ajax/Handler1.ashx?time=" + new Date().getTime(),
        contentType: "application/json; charset=utf-8",
        data: { UserID: 222, Type: "Select_lxl" },
        dataType: "json",
        success: function (data) {
            //alert(data);
            var html_yx = "";
            $.each(data, function (index, item) {
    
                $.each(item, function (i, t) {
                    html_yx = "";
                    if (i == "yx") {
    
                        $.each(t, function (n, t) {
    
                            html_yx += " <div class=\"div_yx\">";
                            html_yx += "               <p class=\"p_up\">";
                            html_yx += "                     <span>" + t.associationName + "</span>";
                            html_yx += "          </p>";
                            html_yx += "              <p class=\"p_down\">";
                            html_yx += t.type == "1" ? "<input type='checkbox' name='xqxh' value='0' checked='checked' style='display:none' />" : "";
                            html_yx += "                     <span>" + (t.state == "1" ? "<span class='yx_zt'>审批中</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type='button' class='but_del' onclick=\"yxbut(" + t.Signupid + ", 'delete')\" value='删除' />" : "<span>已通过</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type='button' class='but_del'  onclick=\"yxbut(" + t.Signupid + ",'update')\" value='退出' />") + "</span>";
                            html_yx += "                  </p>";
                            html_yx += "             </div>";
                        });
                        $(".yx").html(html_yx);
                    }
                    else {
    
                        html_yx += "<div class=\"xqxh_div\">";
                        html_yx += " <h2>" + i + "</h2>";
                        html_yx += "<div class=\"xqxh_list\">";
    
                        $.each(t, function (n, t) {
    
                            html_yx += " <a><i style=\"background:url(image/icon/" + t.pic + ");background-size: 100%;\"></i><span>";
                            html_yx += "            <input type=\"checkbox\" name=\"" + (i == "传统项目" ? "ctxm" : "xqxh") + "\" value=" + t.associationID + " />";
                            html_yx += "            &nbsp;" + t.associationName + "</span></a>";
    
                        })
    
                        html_yx += "</div>";
                        html_yx += "</div>";
    
    
                        $(".kxxh").append(html_yx);
                    }
                });
            });
        },
        error: function () {
            //alert("错误");
        }
    });

    相关文章

      网友评论

          本文标题:C# 拼接json字符串以及ajax遍历二维json

          本文链接:https://www.haomeiwen.com/subject/aabojftx.html