james
james

Reputation: 2663

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.....

Upvotes: 0

Views: 308

Answers (3)

Engineer
Engineer

Reputation: 48813

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.

Upvotes: 1

rjz
rjz

Reputation: 16510

flickr.photos.getInfo returns a single photo in object form ({}).

{
  photo: { 
   owner: 1,
   title: 'photo title',
   // etc.
  }
}

You would only expect to receive numeric keys if (a) they've been assigned to the object (very unusual) or (b) the API method you're using returns an array of objects.

photos: [{
   owner: 1,
   title: 'photo 1'
}, {
   owner: 1,
   title: 'photo 2'
}]

Upvotes: 1

The System Restart
The System Restart

Reputation: 2881

if you want indexing instead of id, server, firm then you can try this

var count = 0;
$.each(data.photo, function(index,item){
      alert(index + ' is at ' + count);
      count++;
 });

Upvotes: 0

Related Questions