如何提取使用JSON API reddit的URL数据 [英] How to extract url data from Reddit API using JSON

查看:246
本文介绍了如何提取使用JSON API reddit的URL数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从影像只能版(Subreddit)中提取的URL,让他们内部的输出< IMG方式> 标签

I am trying to extract the URLs from an image-only subreddit and have them output inside <img> tags.

一直试图破解一起 .getJSON() Flickr的例子从jQuery文档即时了一段时间,我没有得到任何地方。

Been trying to hack together the .getJSON() Flickr example from the jQuery Docs for a while now and I'm not getting anywhere.

下面是我的code:

$.getJSON("http://www.reddit.com/r/pics.json",
        function(data) {
            $.each(data.children, function(i,item){
          $("<img/>").attr("src", url).appendTo("#images");
            if ( i == 3 ) return false;
        });
      });

,并在&LT;身体GT; ,我有&LT; D​​IV ID =图片&GT;&LT; / DIV&GT;

据我了解,我需要使用JSONP,但我不知道该怎么做。有人能指出我朝着正确的方向吗?

I understand that I need to use JSONP, but I don't know how to do it. Can somebody point me in the right direction?

推荐答案

您使用了错误的URL。使用此:

You are using the wrong url. Use this:

$.getJSON("http://www.reddit.com/r/pics/.json?jsonp=?", function(data) { 
    // Do whatever you want with it.. 
});

编辑:在评论基于您的提琴 工作的例子

EDIT : Working example based on your fiddle in the comments.

$.getJSON("http://www.reddit.com/r/pics/.json?jsonp=?", function(data) { 
    $.each(data.data.children, function(i,item){
        $("<img/>").attr("src", item.data.url).appendTo("#images");
    });
});

您应该使用 data.data.children ,而不是 data.children

这篇关于如何提取使用JSON API reddit的URL数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆