jquery - Asp.net MVC Ajax 迭代

标签 jquery asp.net ajax json

我正在使用 $.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/

相关文章:

javascript - 如何隐藏除了span jQuery之外的p标签内的所有内容?

php - 计算日期时间集之间的总时间

jquery - 如何摆脱可折叠菜单的 "blinking"重新加载效果?

c# - PayPal 即时付款通知 (IPN) 不工作 ASP.NET C#

c# - 将月份字符串转换为 int 格式

asp.net - 在 SQL Server 中使用 Redis

php - 通过 PHP 将多维数组 POST 到服务器

javascript - 为什么我需要双击最后一张图片?

javascript - 为什么 google chrome 会给 ebay-api 端点的 ajax get 请求提供错误未知的 url 方案

javascript - 如何在 Ajax 调用 Controller 后显示 View