我正在编写使用 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”的成员(例如),然后只提取结果的那个成员,这样我就可以从它source
和url
价值观。
我天真地尝试了以下方法:
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 结果中检查和检查“大”记录的有效方法是什么?