admin管理员组文章数量:1278789
So I've written some code that searches reddits api based on a query and I want it to display ments as well. I have the following code nested inside my $.getJSON
statement that pulls each title/post based on your search query, and now I want to display the ment tree for each result thats found (hence why I have it nested in my original getJSON statement)
$.getJSON("/" + sub + "/ments/" + id + ".json?", function (data){
$.each(data.data.children, function (i, item) {
var ment = item.data.body
var author = item.data.author
var postment = '<p>[Author]' + author + '<br>' + ment + '</p>'
results.append(postment)
});
});
I have a feeling I may be structuring the $.each
statement wrong or something. I'm just following what I did for the other getJSON statement. Any ideas?
So I've written some code that searches reddits api based on a query and I want it to display ments as well. I have the following code nested inside my $.getJSON
statement that pulls each title/post based on your search query, and now I want to display the ment tree for each result thats found (hence why I have it nested in my original getJSON statement)
$.getJSON("http://www.reddit./r/" + sub + "/ments/" + id + ".json?", function (data){
$.each(data.data.children, function (i, item) {
var ment = item.data.body
var author = item.data.author
var postment = '<p>[Author]' + author + '<br>' + ment + '</p>'
results.append(postment)
});
});
I have a feeling I may be structuring the $.each
statement wrong or something. I'm just following what I did for the other getJSON statement. Any ideas?
- I also noticed on JSONViewer that when I use the url to retrieve ments, it pulls [0] and [1], 0 having the original data for the post, and 1 having the data for the ments. Maybe I need to specify to look at [1]? – mickdeez Commented Feb 5, 2014 at 14:31
1 Answer
Reset to default 9The reddit json contains two objects: the post, and the ments. The ments are located at data[1] This should work:
$.getJSON("http://www.reddit./r/" + sub + "/ments/" + id + ".json?", function (data){
$.each(data[1].data.children, function (i, item) {
var ment = item.data.body
var author = item.data.author
var postment = '<p>[Author]' + author + '<br>' + ment + '</p>'
results.append(postment)
});
});
本文标签: javascriptRetrieving comments from Reddit39s APIStack Overflow
版权声明:本文标题:javascript - Retrieving comments from Reddit's API - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741244719a2364653.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论