webservicesco
webservicesco

Reputation: 7

Manipulating data from a JSON feed to display it

{
  "Group": [
    {
      "name": "HolderOne",
      "operators": [
          {
           "username": "ken",
           "status": 3
          },
         .....etc.....

The JSON feed I am attempting to manipulate has to format above.

I wish to be able to display username and status.

$json = file_get_contents("urlhere");
$obj=json_decode($json);
echo $obj->username;
echo $obj->status;

This obviously doesn't work as they are nested(?) within the feed...I have tried:

$obj->Group[0]->name->operators->username

and

$obj->Group[0]->name->username

to no avail (as well as json_decode with ,true and ['name'], etc).

Am I being particularly dim?

when I do a var dump, the data is being collected from the feed okay.

Upvotes: 0

Views: 1879

Answers (1)

Landon
Landon

Reputation: 4108

The best way to figure this out is to iteratively do print_r's:

print_r($obj)
//prints what you see above
print_r($obj['Group']
//prints the Group Object
print_r($obj['Group'][0])
//prints first element in Group Object
print_r($obj['Group'][0]['operators'])
//etc.....

That's how I find out how to access these deep elements if I get a little stuck. Though it appears to me that you want:

$obj->Group[0]->operators[0]->username

Upvotes: 1

Related Questions