Search code examples
jqueryjsonflickr

Jquery .each() and Flickr Json


I am looping over the JSON returned from Flickr. I am expecting the following would alert 0,1,2,3... etc for the index, but instead it is alerting id,server,farm,etc

$.each(data.photo, function(index,item){
             alert(index);

 });

EDIT BTW I am using the method=flickr.photos.getInfo

var Info = "http://api.flickr.com/services/rest/?method=flickr.photos.getInfo";
                Info += "&api_key=" + "myapikey";
            Info+= "&photo_id="+item.id;
            Info += "&format=json";
            Info+= "&jsoncallback=?";

$.getJSON(Info,loaded);

function loaded(){
$.each(data.photo, function(index,item){
      //Trying to get number of JSON Object (I am getting the info for 52 photos)....
      //I want to alert 0 through 52 here and do some further processing

});

}

EDIT As pointed out by Engineer I should not be looping over this and instead should just be using data.photo.....


Solution

  • data.photo is hashmap, that's why you are getting (key,value) pairs in 'each' callback.

    According to API 'method=flickr.photos.getInfo' returns information of one single photo.This means, data.photo is bunch of properties ((key,value) pairs), and there is no meaning to enumerate those.