tags:

views:

21

answers:

1

I'm a total loss. I have this function to read the Twitter Json. The Json is valid but the value is coming back as 'undefined' when I run it.

$.getJSON("http://twitter.com/users/show.json?screen_name=starbucks&callback=?" , function(data) {
var testing = (data.length);
alert(testing);
})
+3  A: 

data is an object* not an array** so it doesn't have a length property.***

Use a debugger like Firebug, Safari/Chrome dev tools and use this code instead:

$.getJSON("http://twitter.com/users/show.json?screen_name=starbucks&callback=?" ,
    function(data) {
        console.log(data);
    });

and you can see that the data is coming back to you perfectly.

Try this to see what I mean, preferably with a JavaScript console available.


*e.g., something that looks like {key: value, ...}; also known as a hash or an associative array

**e.g., something that looks like: [foo, bar, baz, ...]

*** unless, of course, someone was evil and constructed the object like so:

data = {
    ...
    length: 8675309,
    ...
};
Matt Ball
Aha, ok. And thanks for letting me know about the console. This is a fantastic feature. Iv'e been dealing with alert boxes this whole time.
egfx
@egfx: wow, that's horrible! My life would be miserable without a proper JavaScript debugger.
Matt Ball