I am returning an object to my view using $.getJSON
. The object contains a number of Lists, that I need to iterate over. How can this be done? My 开发者_运维技巧code so far is:
$.getJSON("/Home/GetData",
function (data) {
$.each(data, function (index) {
//access data here for each list in object?
});
});
Thanks.
If I understand what you're asking here, you're getting back JSON object of this form:
// result
{
people: [{...}, {...}, ... , {...}],
places: [{...}, {...}, ... , {...}],
...
}
You should first iterate over your returned JSON object and then iterate over each list's items individually. Something along these lines:
$.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
});
}
});
When using $.each, the signature is:
$.each(data, function (index, item) {
//access data here for each list in object?
});
So the item can be accessed directly. Also, I think a list should be pushed up as an array, so you can have any number of $.each statements as you need. The next inner array would be available via:
$.each(item.SubList, function(si, sitem) {
});
If you post the data structure pushed, we could help more. Also, the object you are returning, is it an anonymous class or strongly-typed? Are you returning a JsonResult?
HTH.
精彩评论