我正在使用 $.getJSON
将对象返回到我的 View 。该对象包含许多列表,我需要对其进行迭代。如何才能做到这一点?到目前为止我的代码是:
$.getJSON("/Home/GetData",
function (data) {
$.each(data, function (index) {
//access data here for each list in object?
});
});
谢谢。
最佳答案
如果我明白你在这里问的是什么,你将返回这种形式的 JSON 对象:
// result
{
people: [{...}, {...}, ... , {...}],
places: [{...}, {...}, ... , {...}],
...
}
您应该首先迭代返回的 JSON 对象,然后单独迭代每个列表的项目。大致如下:
$.getJSON("/Home/GetData", function (data) {
// iterate over lists in an object
for(var list in data)
{
// list variable holds the name of the list (ie. "people")
// iterate over list items
$.each(data[list], function (index) {
// do something with this list item
});
}
});
关于jquery - Asp.net MVC Ajax 迭代,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6581702/