doforumda Posted September 24, 2011 Share Posted September 24, 2011 How can I get the comments from below json data? { "data": [ { "id":"123", "from":{"name":"name","id":"12"}, "message":"Message", "comments": { "data": [ { "id":"342", "from":{"name":"name","id":"32"}, "message":"comment message 1" }, { "id":"341", "from":{"name":"name","id":"21"}, "message":"comment message 2" } ], "count":2 } }, { "id":"143", "from":{"name":"name","id":"52"}, "message":"Message", "comments": { "data": [ { "id":"362", "from":{"name":"name","id":"72"}, "message":"comment message 1" }, { "id":"341", "from":{"name":"name","id":"41"}, "message":"comment message 2" } ], "count":2 } } ] } I know how to get id, from and message. but I do not know how can I get data inside comments. here is my jquery code $.getJSON(newsfeed_url,function(d) { $.each(d.data, function(i,res) { html += "<div class='container'>"; html += "<li>"+ res.from.name +"</li>"; html += "<li class='message'>" + res.message + "</li>"; html += "<li>Comments: " + res.comments.count + "</li>"; $.each(res.comments.data, function(i, comment) { html += "<li class=''>" + comment.message + "</li>"; }); html += "</div>"; }); html += "</ul>"; $("#contents").html(html); }); my current code does get res.from.name, res.comments.count but it does not get data inside comments i.e. res.comments.data. how can I achieve it? Link to comment https://forums.phpfreaks.com/topic/247787-how-to-get-json-data-using-jquery/ Share on other sites More sharing options...
Adam Posted September 25, 2011 Share Posted September 25, 2011 The data is in an array, so you would need to access them through the index: res.comments.data[0].id res.comments.data[1].id Or using the count property you have, you could loop through them: for (i = 0; i < res.comments.count; i++) { // res.comments.data[i].id } Link to comment https://forums.phpfreaks.com/topic/247787-how-to-get-json-data-using-jquery/#findComment-1272596 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.