试图调整$.get
JSON Flickr示例:
$.getJSON("http://api.flickr.com/services/@R_502_349@s/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,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调用?
解决方法
没关系,我知道了.对于那些有兴趣的人,这样解释:
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,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方法签名.