jQuery $.getJSON - 如何解析 flickr.photos.search REST API 调用?

发布于 2024-08-26 10:22:36 字数 1131 浏览 7 评论 0原文

尝试调整 $.getJSON Flickr 示例:

$.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?tags=cat&tagmode=any&format=json&jsoncallback=?",
    function(data){
    $.each(data.items, function(i,item){
        $("<img/>").attr("src", item.media.m).appendTo("#images");
        if ( i == 3 ) return false;
    });
});

从 flickr.photos.search REST API 方法读取,但此调用的 JSON 响应不同。

这就是我到目前为止所做的:

var url = "http://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=YOURAPIKEYHERE&tags=yokota+air+base&safe_search=1&per_page=20";
var src;
$.getJSON(url + "&format=json&jsoncallback=?", function(data){
    $.each(data.photos, function(i,item){
        src = "http://farm"+ item.photo.farm +".static.flickr.com/"+ item.photo.server +"/"+ item.photo.id +"_"+ item.photo.secret +"_m.jpg";
        $("<img/>").attr("src", src).appendTo("#images");
        if ( i == 3 ) return false;
    });
});

我想我没有正确构建图像 src。找不到任何有关如何根据 JSON 响应构建图像 src 的文档。 如何解析 flickr.photos.search REST API 调用?

Trying to adapt the $.getJSON Flickr example:

$.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?tags=cat&tagmode=any&format=json&jsoncallback=?",
    function(data){
    $.each(data.items, function(i,item){
        $("<img/>").attr("src", item.media.m).appendTo("#images");
        if ( i == 3 ) return false;
    });
});

to read from the flickr.photos.search REST API method, but the JSON response is different for this call.

This is what I've done so far:

var url = "http://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=YOURAPIKEYHERE&tags=yokota+air+base&safe_search=1&per_page=20";
var src;
$.getJSON(url + "&format=json&jsoncallback=?", function(data){
    $.each(data.photos, function(i,item){
        src = "http://farm"+ item.photo.farm +".static.flickr.com/"+ item.photo.server +"/"+ item.photo.id +"_"+ item.photo.secret +"_m.jpg";
        $("<img/>").attr("src", src).appendTo("#images");
        if ( i == 3 ) return false;
    });
});

I guess I'm not building the image src correctly. Couldn't find any documentation on how to build the image src, based on what the JSON response is. How do you parse a flickr.photos.search REST API call?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

明媚如初 2024-09-02 10:22:37

要访问 Flickr API,您必须使用 https://

var url = "https://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=YOURAPIKEYHERE&tags=yokota+air+base&safe_search=1&per_page=20";
var src;

To access the Flickr API you must use https: //:

var url = "https://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=YOURAPIKEYHERE&tags=yokota+air+base&safe_search=1&per_page=20";
var src;
清旖 2024-09-02 10:22:37

jQuery.getJSON() 的 jQuery API 文档在这里有一个有用的示例:http://api.jquery。 com/jquery.getjson/

文档提供了完整的文件作为如何解析API调用的示例;它从 Flickr JSONP API 加载雷尼尔山的四张最新图片。我将在此处重新打印该文件,作为可能有用的附加上下文,特别是对于那些刚开始使用 API 的人。

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>jQuery.getJSON demo</title>
  <style>
  img {
    height: 100px;
    float: left;
  }
  </style>
  <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>

<div id="images"></div>

<script>
(function() {
  var flickerAPI = "http://api.flickr.com/services/feeds/photos_public.gne?jsoncallback=?";
  $.getJSON( flickerAPI, {
    tags: "mount rainier",
    tagmode: "any",
    format: "json"
  })
    .done(function( data ) {
      $.each( data.items, function( i, item ) {
        $( "<img>" ).attr( "src", item.media.m ).appendTo( "#images" );
        if ( i === 3 ) {
          return false;
        }
      });
    });
})();
</script>

</body>
</html>

The jQuery API documentation for jQuery.getJSON() has a helpful example here: http://api.jquery.com/jquery.getjson/

The documentation provides a complete file as an example of how to parse the API call; it loads the four most recent pictures of Mount Rainier from the Flickr JSONP API. I'll reprint the file here as additional context that might be helpful, particularly for those new to using API's.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>jQuery.getJSON demo</title>
  <style>
  img {
    height: 100px;
    float: left;
  }
  </style>
  <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>

<div id="images"></div>

<script>
(function() {
  var flickerAPI = "http://api.flickr.com/services/feeds/photos_public.gne?jsoncallback=?";
  $.getJSON( flickerAPI, {
    tags: "mount rainier",
    tagmode: "any",
    format: "json"
  })
    .done(function( data ) {
      $.each( data.items, function( i, item ) {
        $( "<img>" ).attr( "src", item.media.m ).appendTo( "#images" );
        if ( i === 3 ) {
          return false;
        }
      });
    });
})();
</script>

</body>
</html>
野却迷人 2024-09-02 10:22:36

没关系,我明白了。对于那些感兴趣的人,它的解析方式如下:

var url = "http://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=YOURAPIKEYHERE&tags=yokota+air+base&safe_search=1&per_page=20";
var src;
$.getJSON(url + "&format=json&jsoncallback=?", function(data){
    $.each(data.photos.photo, function(i,item){
        src = "http://farm"+ item.farm +".static.flickr.com/"+ item.server +"/"+ item.id +"_"+ item.secret +"_m.jpg";
        $("<img/>").attr("src", src).appendTo("#images");
        if ( i == 3 ) return false;
    });
});

请注意,.photo 已移至 $.each 方法签名。

Nevermind, I got it. For those that are interested, it's parsed like so:

var url = "http://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=YOURAPIKEYHERE&tags=yokota+air+base&safe_search=1&per_page=20";
var src;
$.getJSON(url + "&format=json&jsoncallback=?", function(data){
    $.each(data.photos.photo, function(i,item){
        src = "http://farm"+ item.farm +".static.flickr.com/"+ item.server +"/"+ item.id +"_"+ item.secret +"_m.jpg";
        $("<img/>").attr("src", src).appendTo("#images");
        if ( i == 3 ) return false;
    });
});

Notice the .photo was moved to the $.each method signature.

油焖大侠 2024-09-02 10:22:36

这可以通过选择 'url_m' extras 参数和 per_page 参数来简化...

extras=url_m&per_page=4

那么您所需要的就是循环中的这个...

$("<img/>").attr("src", item.url_m).appendTo("#images");

This could be simplified by choosing the 'url_m' extras parameter and per_page parameter...

extras=url_m&per_page=4

Then all you need is this within the loop...

$("<img/>").attr("src", item.url_m).appendTo("#images");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文