Mart Haarman Mart Haarman - 23 days ago 6
Javascript Question

getJSON response handling

This is my Javascript code to handle the response of a getJSON:

$.getJSON(url, {
userID:userID,
unique:unique
}, function(response) {
alert(JSON.stringify(response));
if(response.connection_status == true) {
alert(response.connected_friends[0].id);
}
});


The first alert does fire and gives me this:

{ "connection_status":true,
"hasResults":true,
"connected_friends":{
"id":"1055",
"name":"My Name"
}
}


Why is my 2nd alert not showing?

what am I missing here?

Answer

You should do

response.connected_friends["id"]

or

response.connected_friends.id

as it is an object not an Array