Options

API: Is this a bug or feature?

komykomy Registered Users Posts: 19 Big grins
Hi,

I am using the 1.2.0 JSON API to fetch a list of subcategories for a given category using Javascript. However, the data structure returned from the API does not look quite right to me. Here is what is returning from the API.
{"stat":"ok",
 "method":"smugmug.subcategories.get",
 "SubCategories":{
                         "311625":{"id":311625,"Title":"Subcategory 1"},
                         "313457":{"id":313457,"Title":"Subcategory 2"}
                        }
}
I would expect the API retuning subcategories in an array structure like:
{"stat":"ok",
  "method":"smugmug.subcategories.get",
  "SubCategories":[
                          {"id":311625,
                           "Title":"Subcategory 1"},
                          {"id":313457,
                           "Title":"Subcategory 2"}
                        ]
}

If what the current API returns is in a correct structure, how should I loop through each subcategory?

Thanks,
Kevin

Comments

  • Options
    devbobodevbobo Registered Users, Retired Mod Posts: 4,339 SmugMug Employee
    edited April 28, 2007
    Hi Kevin,

    That's the way we decided to do for all methods that return sets of data.

    The results can be easily iterated using the for command like below...
    for (var i in x.SubCategories) {
      alert(x.SubCategories[i].Title)
    }
    
    where x is the variable that the JSON structure was assigned to.

    The advantage of this method is that if you know the SubCategory ID that you want, you don't need to iterate the array to find the index, it can be accessed directly using the SubCategory ID.
    eg. SubCategories.311625.Title

    I hope this makes sense.

    Cheers,

    David
    David Parry
    SmugMug API Developer
    My Photos
  • Options
    komykomy Registered Users Posts: 19 Big grins
    edited April 29, 2007
    Thanks, that makes sense. Thanks!

    Kevin
Sign In or Register to comment.