怎样的jQuery返回我环路JSON数据? [英] How do I loop JSON data returned by jquery?
本文介绍了怎样的jQuery返回我环路JSON数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可能重复:结果
<一href=\"http://stackoverflow.com/questions/5953761/how-do-i-return-json-and-loop-through-the-returned-json-in-jquery-in-mvc-app\">How我通过MVC应用程序中的jQuery返回的JSON返回JSON和循环?
这是MVC控制器返回我的数据,我得到这个在我成功的回调:
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 } }
]
控制器:
[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);
}
});
也试过:
$.each(result["comments"], function (key, value) {
alert('comment found');
});
我环行和放大器
如何能;访问每个属性的值?
How can I loop the rows & access each attribute's value?
推荐答案
您可以只使用一个简单的为
循环:
You could just use a simple for
loop:
for (var i = 0, len = results.length; i < len; i++) {
// do something with results[i].text
}
编辑:如果您首先需要一个JSON字符串转换为JavaScript对象,然后在循环之前,您应该:
If you need to first convert a JSON string to a Javascript object then before the loop you should:
results = JSON.parse(results);
这篇关于怎样的jQuery返回我环路JSON数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文