开发者

How do I loop JSON data returned by jquery? [duplicate]

This question already has answers here: Closed 11 years ago.

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], fu开发者_StackOverflow中文版nction () {
                   $.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?


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);


$.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


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?


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); }) });


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]);   
        }     
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜