Search code examples
javascriptjqueryjsongetgetjson

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?


Solution

  • You should do

    response.connected_friends["id"]

    or

    response.connected_friends.id

    as it is an object not an Array