1

The response I'm getting back from PHP using an AJAX call:

$.ajax({
  url: 'getit.php',
  type: 'POST',
  dataType: 'json',
  data: {id_token: my_token},
  success: function(data) {
  //stuff
  }
  });

gives me something like this when I console.log it:

email: "me@example.co"
email_verified: true
first_name: "Bob"
permissions: Object
   client_1001: Object
      client_id: "121434"
      role: "full"
      table_name: "5tyyd"
   client_1002: Object
      client_id: "45638"
      role: "full"
      table_name: "df823"

If I do something like this:

$('.last_name').text(data.first_name);

and

<div class="last_name"></div>

This gives me back Bob just as expected.

What I need to get is a list of permissions (e.g., client_1001) and the pieces of data under each permission. I can't figure out how to do though.

This is where I'm stuck:

$('.last_name').text(data.permissions.WHATGOESHERE?);

I need to fill in the WHATGOESHERE? part. It's basically an object in an object, but I'm not sure how to parse it.

jonmrich
  • 4,233
  • 5
  • 42
  • 94
  • possible duplicate of [jQuery JSON looping through nested objects](http://stackoverflow.com/questions/8553539/jquery-json-looping-through-nested-objects) –  Feb 16 '15 at 03:49

1 Answers1

2

As you're using jQuery, give this a try :

$.each(data.permissions, function (index, value) {
    console.log(index);            // "client_0001"
    console.log(value.table_name); // "5tyyd"
});

The idea is to iterate through the response, as if you were using foreach() in PHP.

Didier Sampaolo
  • 2,566
  • 4
  • 24
  • 34