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

该博客主要介绍了Handler1.ashx中的处理方法,用于根据UserID和Type参数获取并返回不同的JSON数据。数据包括已报名的兴趣协会、可选兴趣协会和传统项目,内容涉及协会名称、类型、状态等信息。同时,博客还展示了使用jQuery AJAX进行数据请求和处理的示例。
摘要由CSDN通过智能技术生成

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 += "

";

html_yx += "

";

html_yx += " " + t.associationName + "";

html_yx += "

";

html_yx += "

";

html_yx += t.type == "1" ? "" : "";

html_yx += " " + (t.state == "1" ? "审批中     " : "已通过     ") + "";

html_yx += "

";

html_yx += "

";

});

$(".yx").html(html_yx);

}

else {

html_yx += "

";

html_yx += "

" + i + "

";

html_yx += "

";

$.each(t, function (n, t) {

html_yx += " ";

html_yx += " ";

html_yx += "  " + t.associationName + "";

})

html_yx += "

";

html_yx += "

";

$(".kxxh").append(html_yx);

}

});

});

},

error: function () {

//alert("错误");

}

});

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值