0

我正在编写使用 Flickr API 的代码,典型的请求返回如下 JSON 结果:

{ "sizes": { "canblog": 0, "canprint": 0, "candownload": 1, 
    "size": [
        { "label": "Square", "width": 75, "height": 75, "source": "http:\/\/farm3.staticflickr.com\/2811\/9227026248_53e958a083_s.jpg", "url": "http:\/\/www.flickr.com\/photos\/l0rdshrek\/9227026248\/sizes\/sq\/", "media": "photo" },
        { "label": "Large Square", "width": "150", "height": "150", "source": "http:\/\/farm3.staticflickr.com\/2811\/9227026248_53e958a083_q.jpg", "url": "http:\/\/www.flickr.com\/photos\/l0rdshrek\/9227026248\/sizes\/q\/", "media": "photo" },
        { "label": "Thumbnail", "width": 100, "height": 75, "source": "http:\/\/farm3.staticflickr.com\/2811\/9227026248_53e958a083_t.jpg", "url": "http:\/\/www.flickr.com\/photos\/l0rdshrek\/9227026248\/sizes\/t\/", "media": "photo" },
        { "label": "Small", "width": "240", "height": "180", "source": "http:\/\/farm3.staticflickr.com\/2811\/9227026248_53e958a083_m.jpg", "url": "http:\/\/www.flickr.com\/photos\/l0rdshrek\/9227026248\/sizes\/s\/", "media": "photo" },
        { "label": "Small 320", "width": "320", "height": "240", "source": "http:\/\/farm3.staticflickr.com\/2811\/9227026248_53e958a083_n.jpg", "url": "http:\/\/www.flickr.com\/photos\/l0rdshrek\/9227026248\/sizes\/n\/", "media": "photo" },
        { "label": "Medium", "width": "500", "height": "375", "source": "http:\/\/farm3.staticflickr.com\/2811\/9227026248_53e958a083.jpg", "url": "http:\/\/www.flickr.com\/photos\/l0rdshrek\/9227026248\/sizes\/m\/", "media": "photo" },
        { "label": "Medium 640", "width": "640", "height": "480", "source": "http:\/\/farm3.staticflickr.com\/2811\/9227026248_53e958a083_z.jpg", "url": "http:\/\/www.flickr.com\/photos\/l0rdshrek\/9227026248\/sizes\/z\/", "media": "photo" },
        { "label": "Large", "width": "700", "height": "525", "source": "http:\/\/farm3.staticflickr.com\/2811\/9227026248_53e958a083_b.jpg", "url": "http:\/\/www.flickr.com\/photos\/l0rdshrek\/9227026248\/sizes\/l\/", "media": "photo" },
        { "label": "Original", "width": "700", "height": "525", "source": "http:\/\/farm3.staticflickr.com\/2811\/9227026248_44fe99eea1_o.jpg", "url": "http:\/\/www.flickr.com\/photos\/l0rdshrek\/9227026248\/sizes\/o\/", "media": "photo" }
    ] }, "stat": "ok" }

我想做的是,使用 JavaScript 和 jQuery,检查结果是否包含size一个label值为“Large”的成员(例如),然后只提取结果的那个成员,这样我就可以从它sourceurl价值观。

我天真地尝试了以下方法:

var photoURL, thumbURL, photoLink;
$.ajax({ //inner request to Flickr for photo sizes
    type: 'GET',  
    url: getSizeUrl,  
    dataType: 'json', 
    async: true,  
    success: function(sizeData){
        var sizeResults = sizeData.sizes.size;

        if(sizeResults[label="Large"]) {
            photoURL = sizeResults[label="Large"].source;
        }
}});

但是sizeResults[label="Large"]语法似乎无效。我已经找了一个多小时试图找到正确的方法来做到这一点,但我必须使用错误的搜索词,因为我找不到任何这种数据提取的例子。可以做到吗?[如果另一个 Stack Overflow 问题已经很好地涵盖了这一点,请指出它,我将删除这个重复项。]

如果不能以这种方式做事,那么从上述 JSON 结果中检查和检查“大”记录的有效方法是什么?

4

3 回答 3

1

我会用$.grep这个。它干净且易于使用。

var a = $.grep(data.sizes.size, function (size) { 
  // here you'll get an object of data. You'll have to use the filter condition here   
  return size["label"] === "Large";
})[0]; // you'll get an [{}] if you don't use [0] in the end. 
// now, you'll get just {} because of the [0] in the end.

从这里开始,你可以说,

var photoURL = a.source;

data.sizes.size这将返回其中有一个名为“Large”的标签的每个元素。在此处的文档中查看更多信息

演示:http: //jsfiddle.net/vLaUB/1/

于 2013-07-07T00:04:20.080 回答
0

您可以将它们全部放入一个对象数组中,然后在对象上设置某种键,或者您可以循环遍历结果:

$.each(sizeResults, function(index, value) { 
  if (value.label == "Large") {
    photoURL = value.source;
    return false; // breaks out of the loop.
  }
});

当然效率不高,但它应该足够快......

于 2013-07-07T00:04:12.130 回答
0

使用纯 JavaScript:

var large = sizeResults.filter(function(x) { return x.label === 'Large'; })[0];
large; // =>  { "label": "Large", "width": "700", "height": "525" ... }
于 2013-07-07T00:19:39.690 回答