Something like this?
function myjsoncallback(obj) {
var result = [];
for (var key in obj) {
result.push(obj[key]);
}
// result is now an array of the input object's values.
}
If you're using jQuery 1.6+, jQuery's $.map()
function now works against objects and simplifies the syntax a bit more (though it's doing the same thing under the hood):
function myjsoncallback(obj) {
var result = $.map(obj, function(value, key) {
return value;
});
}
To use this with a jQuery $.getJSON call to a service that properly supports JSONP, you can do something like this:
$.getJSON('http://some.api.com/x/y/x?callback=?', function(response) {
var values = $.map(response, function(value, key) {
return value;
});
// Do something with "values" here.
});
When you specify callback=?
instead of hard coding your own callback function name, jQuery will automatically substitute a uniquely named one for each request (that way you don't have to worry about keeping straight which callback execution matches which request if you end up having more than one simultaneous request). Since it does that for you, you can just pass in an anonymous function for the callback parameter and not worry about setting up a standalone function at all.