如何循环jquery返回的JSON数据?
这是 MVC 控制器返回的数据,我在成功回调中得到此数据:
[{ "_id" : { "$oid" : "4dc8" }, "eid" : { "$oid" : "4da" }, "user" : "bob", "text" : "First comment!!", "dt" : { "$date" : 1304966277978 } },
{ "_id" : { "$oid" : "4dc8" }, "eid" : { "$oid" : "4da" }, "user" : "bob", "text" : "Second comment!!", "dt" : { "$date" : 1304966347677 } },
{ "_id" : { "$oid" : "4dc8" }, "eid" : { "$oid" : "4da" }, "user" : "bob", "text" : "Third comment!!", "dt" : { "$date" : 1304966493240 } }
]
控制器:
[HttpGet]
public JsonResult GetComments(params...)
{
return Json(new { comments = GetFromDB().ToJson() }, JsonRequestBehavior.AllowGet);
}
问题: 我尝试了几种循环行的方法。但一切似乎无限循环。
$.ajax(
{
type: "GET",
url: "/comment/GetComments",
dataType: "json",
data: "app=" + app + "&eid=" + eid + "&pg=" + pg + "&pgs=" + pgs,
success: function (result) {
$.each(result[comments], function () {
$.each(this, function (k, v) {
alert('this a column or attribute');
});
alert('end of row');
});
},
error: function (req, status, error) {
alert('Error=' + error + ' & Status=' + status);
}
});
还尝试过:
$.each(result["comments"], function (key, value) {
alert('comment found');
});
如何循环行和行?访问每个属性的值?
Possible Duplicate:
How do I return JSON and loop through the returned json in jQuery in MVC app?
This is my data returned by MVC controller and I get this in my success callback:
[{ "_id" : { "$oid" : "4dc8" }, "eid" : { "$oid" : "4da" }, "user" : "bob", "text" : "First comment!!", "dt" : { "$date" : 1304966277978 } },
{ "_id" : { "$oid" : "4dc8" }, "eid" : { "$oid" : "4da" }, "user" : "bob", "text" : "Second comment!!", "dt" : { "$date" : 1304966347677 } },
{ "_id" : { "$oid" : "4dc8" }, "eid" : { "$oid" : "4da" }, "user" : "bob", "text" : "Third comment!!", "dt" : { "$date" : 1304966493240 } }
]
Controller:
[HttpGet]
public JsonResult GetComments(params...)
{
return Json(new { comments = GetFromDB().ToJson() }, JsonRequestBehavior.AllowGet);
}
Problem:
I tried several ways to loop the rows. But all seems infinite loop.
$.ajax(
{
type: "GET",
url: "/comment/GetComments",
dataType: "json",
data: "app=" + app + "&eid=" + eid + "&pg=" + pg + "&pgs=" + pgs,
success: function (result) {
$.each(result[comments], function () {
$.each(this, function (k, v) {
alert('this a column or attribute');
});
alert('end of row');
});
},
error: function (req, status, error) {
alert('Error=' + error + ' & Status=' + status);
}
});
Also tried:
$.each(result["comments"], function (key, value) {
alert('comment found');
});
How can I loop the rows & access each attribute's value?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您应该能够使用它逐步浏览行和值。
最好的,
T
You should be able to step through the rows and values with this.
Best,
T
这是今天另一篇文章的重复 - 你发布的? :)
如何在 MVC 应用程序中返回 JSON 并循环遍历 jQuery 中返回的 json?
This is a duplicate from another post today - that you posted? : )
How do I return JSON and loop through the returned json in jQuery in MVC app?
您的第一个问题是 JSON 中没有
result["comments"]
字段。你会得到一个数组,我认为其中就是评论本身。所以你需要对此进行迭代。类似的东西应该可以工作,我刚刚在浏览器中尝试过。以下代码迭代行,然后迭代每行的属性:
Your first problem is that there is no
result["comments"]
field in your JSON. You're getting an array back, of what I assume is the comments themselves. So you need to iterate on that. Something likeshould work, I just tried it in the browser. The following code iterates through the rows, and then iterates through the attributes of each row:
您可以使用一个简单的
for
循环:查看示例 →
编辑:如果您需要首先将 JSON 字符串转换为 Javascript 对象,然后在循环之前您应该:
You could just use a simple
for
loop:See example →
EDIT: If you need to first convert a JSON string to a Javascript object then before the loop you should: