jQuery $.getJSON - How do I parse a flickr.photos.search REST API call?

后端 未结 4 907
借酒劲吻你
借酒劲吻你 2021-01-31 06:15

Trying to adapt the $.getJSON Flickr example:

$.getJSON(\"http://api.flickr.com/services/feeds/photos_public.gne?tags=cat&tagmode=any&format=json&jso         


        
相关标签:
4条回答
  • 2021-01-31 06:40

    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.

    0 讨论(0)
  • 2021-01-31 06:41

    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;
    
    0 讨论(0)
  • 2021-01-31 06:50

    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");
    
    0 讨论(0)
  • 2021-01-31 06:55

    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>
    
    0 讨论(0)
提交回复
热议问题