如何循环jquery返回的JSON数据?

发布于 2024-11-05 22:31:05 字数 1809 浏览 0 评论 0原文

可能的重复:
如何在 MVC 应用程序中返回 JSON 并循环遍历 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

一个人的夜不怕黑 2024-11-12 22:31:06
$.getJSON("YourControllerHere.XXX",{}, function(data){
    $.each(data, function(key, val) {
        $('#someLocation').append('<li>' + val.text + '</li>'); //This is just an example. You can do something with each row/ value pair here.
    });
});

您应该能够使用它逐步浏览行和值。

最好的,

T

$.getJSON("YourControllerHere.XXX",{}, function(data){
    $.each(data, function(key, val) {
        $('#someLocation').append('<li>' + val.text + '</li>'); //This is just an example. You can do something with each row/ value pair here.
    });
});

You should be able to step through the rows and values with this.

Best,

T

古镇旧梦 2024-11-12 22:31:06

这是今天另一篇文章的重复 - 你发布的? :)

如何在 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?

恰似旧人归 2024-11-12 22:31:06

您的第一个问题是 JSON 中没有 result["comments"] 字段。你会得到一个数组,我认为其中就是评论本身。所以你需要对此进行迭代。类似的东西

$.each(result, function(k, v) { console.log(v.user); });

应该可以工作,我刚刚在浏览器中尝试过。以下代码迭代行,然后迭代每行的属性:

$.each(foo, function(k, row) { $.each(row, function(attr, value) { console.log(attr, value); }) });

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 like

$.each(result, function(k, v) { console.log(v.user); });

should work, I just tried it in the browser. The following code iterates through the rows, and then iterates through the attributes of each row:

$.each(foo, function(k, row) { $.each(row, function(attr, value) { console.log(attr, value); }) });
短暂陪伴 2024-11-12 22:31:06
for(var key in json)
    for(var key in (obj = json[key]))
        {
            //obj holds the current object in the json array
            console.log(obj[key]);   
        }     
for(var key in json)
    for(var key in (obj = json[key]))
        {
            //obj holds the current object in the json array
            console.log(obj[key]);   
        }     
ゞ花落谁相伴 2024-11-12 22:31:05

您可以使用一个简单的 for 循环:

for (var i = 0, len = results.length; i < len; i++) {
    // do something with results[i].text
}

查看示例 →


编辑:如果您需要首先将 JSON 字符串转换为 Javascript 对象,然后在循环之前您应该:

results = JSON.parse(results);

You could just use a simple for loop:

for (var i = 0, len = results.length; i < len; i++) {
    // do something with results[i].text
}

See example →


EDIT: If you need to first convert a JSON string to a Javascript object then before the loop you should:

results = JSON.parse(results);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文