jQuery, JSON, Flickr API

谁说我不能喝 提交于 2019-12-21 06:24:24

问题


    $.ajax({
    url: "http://api.flickr.com/services/rest/?method=flickr.photos.getSizes&format=json&api_key=708f179518b2093d23f0aef284b565a4&photo_id=6115633659&jsoncallback=?",
    type: "GET",
    cache: true,
    dataType:'jsonp',
    success: function (data) {           

    var largeSRC = data.sizes.size.Original;

    alert(largeSRC);

    }
});

So yeah, basically, I have no clue how to... I don't even know how to term it. I am trying to to get the URL for the original size. http://www.flickr.com/services/api/explore/flickr.photos.getSizes

jsonFlickrApi({ "sizes": { "canblog": 0, "canprint": 0, "candownload": 1, 
"size": [
  { "label": "Square", "width": 75, "height": 75, "source": "http:\/\/farm7.static.flickr.com\/6068\/6115633659_500f8bbd74_s.jpg", "url": "http:\/\/www.flickr.com\/photos\/b34u_h4r13y_n47h4n_7h0m45\/6115633659\/sizes\/sq\/", "media": "photo" },
  { "label": "Thumbnail", "width": 100, "height": 66, "source": "http:\/\/farm7.static.flickr.com\/6068\/6115633659_500f8bbd74_t.jpg", "url": "http:\/\/www.flickr.com\/photos\/b34u_h4r13y_n47h4n_7h0m45\/6115633659\/sizes\/t\/", "media": "photo" },
  { "label": "Small", "width": "240", "height": "159", "source": "http:\/\/farm7.static.flickr.com\/6068\/6115633659_500f8bbd74_m.jpg", "url": "http:\/\/www.flickr.com\/photos\/b34u_h4r13y_n47h4n_7h0m45\/6115633659\/sizes\/s\/", "media": "photo" },
  { "label": "Medium", "width": "500", "height": "331", "source": "http:\/\/farm7.static.flickr.com\/6068\/6115633659_500f8bbd74.jpg", "url": "http:\/\/www.flickr.com\/photos\/b34u_h4r13y_n47h4n_7h0m45\/6115633659\/sizes\/m\/", "media": "photo" },
  { "label": "Medium 640", "width": "640", "height": "424", "source": "http:\/\/farm7.static.flickr.com\/6068\/6115633659_500f8bbd74_z.jpg", "url": "http:\/\/www.flickr.com\/photos\/b34u_h4r13y_n47h4n_7h0m45\/6115633659\/sizes\/z\/", "media": "photo" },
  { "label": "Large", "width": "1024", "height": "678", "source": "http:\/\/farm7.static.flickr.com\/6068\/6115633659_500f8bbd74_b.jpg", "url": "http:\/\/www.flickr.com\/photos\/b34u_h4r13y_n47h4n_7h0m45\/6115633659\/sizes\/l\/", "media": "photo" },
  { "label": "Original", "width": "1440", "height": "954", "source": "http:\/\/farm7.static.flickr.com\/6068\/6115633659_cdaca906e1_o.jpg", "url": "http:\/\/www.flickr.com\/photos\/b34u_h4r13y_n47h4n_7h0m45\/6115633659\/sizes\/o\/", "media": "photo" }
] }, "stat": "ok" })

Please help me escape from this horrible horrible guessing game.

Thanks.


回答1:


Use

$.ajax({
    url: "http://api.flickr.com/services/rest/?method=flickr.photos.getSizes&format=json&api_key=708f179518b2093d23f0aef284b565a4&photo_id=6115633659&jsoncallback=?",
    type: "GET",
    cache: true,
    dataType: 'jsonp',
    success: function(data) {

        var sizelist = data.sizes.size;
        for (var i = 0; i < sizelist.length; i++) {
            if (sizelist[i].label == 'Original') largeSRC = sizelist[i].source;
        }

        alert(largeSRC);

    }
});

Demo at http://jsfiddle.net/NGr23/

The problem is that size is an array. So you need to iterate over its elements to find the one with label of 'Original' ..




回答2:


Try this one http://jsfiddle.net/genesis/TyDHK/5/

 $.ajax({
    url: "http://api.flickr.com/services/rest/?method=flickr.photos.getSizes&format=json&api_key=708f179518b2093d23f0aef284b565a4&photo_id=6115633659&jsoncallback=?",
    type: "GET",
    cache: true,
    dataType: 'jsonp',
    success: function(data) {

        var largeSRC = data.sizes.size[6].width + ' ' + data.sizes.size[6].height ;

        alert(largeSRC);

    }
});



回答3:


Try this.

var url = "http://api.flickr.com/services/rest/?method=flickr.photos.getSizes&format=json&api_key=708f179518b2093d23f0aef284b565a4&photo_id=6115633659&jsoncallback=?";
var largeSRC = "";
$.getJSON(url, function(data) {
    $.each(data.sizes.size, function(i, datum) {
        $.each(datum, function(key, value) {
            if (key === "label" && value === "Original") {
                largeSRC = datum.source;
                alert(largeSRC);
            }
        });
    });
});

Demo: http://jsfiddle.net/naveen/PZNT6/



来源:https://stackoverflow.com/questions/7311538/jquery-json-flickr-api

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!