views:

38

answers:

2

I'm playing around with the twitter API and it's returning the following:

[ 12345, 12345, 12345, 12345 ]

How do I go about parsing this into an array or object?

I can't see how to do it using $.getJSON as there are no keys to reference the data with.

Any help would be greatly appreciated, cheers.

+2  A: 

You won't need keys in this case, in your success function just loop through they array, for example:

$.getJSON(url, function(data) {
  $.each(data, function(i, id) {
    alert('ID #' + i + ' is ' + id);
  });
});

They point is the object might already be an array, so just loop through it, it doesn't need to be data.key[0], it may just be data[0], which is what you have here :)

Nick Craver
See http://json.org for an in-depth explanation of everything you'll encounter.
MvanGeest
Thanks I tried the code out and it returns "a is null" using jQuery 1.4.2. Looking at firebug it's returning a 200 OK so there is something there.
digital
@digital - That's a minified error, saying that `data` itself is null. Are you viewing the response in your console, or trying the same URL in your browser manually? If you're doing it manually, you might be hitting cross-domain issues in your actual AJAX request that you're unaware of...e.g. does your URL contain `?callback=?` to trigger JSONP behavior?
Nick Craver
A: 

you could use that return to create an array.

$arr = [ 12345, 12345, 12345, 12345 ]

Zane Edward Dockery