Kenny
Kenny

Reputation: 1

Parsing JSON results

I understand how to parse json with PHP, however I don't understand how to read it with the eye. Can someone please help me understanad this?

Here is my code

<?php
$json = file_get_contents('json.txt');
$json_output = json_decode($json);

foreach ( $json_output->query as $stf )
{
    echo "{$stf->response->domains->name}\n";
}
?>

Here is a sample of the json result

{ "query" : { "host" : "test.com",
      "tool" : "pro"
    },
  "response" : { "domain_count" : "13",
      "domains" : [ { "last_resolved" : "2012-01-11",
            "name" : "test1.com"
          },
          { "last_resolved" : "2012-01-11",
            "name" : "test2.com"
          },

As you can see I tried query->response->domains->name and it didn't work.

How would I tried name?

Thank you in advance

Upvotes: 0

Views: 190

Answers (3)

Brandan
Brandan

Reputation: 14983

If you're trying to read it by eye, it might help to reformat:

{
  "query" : {
    "host" : "test.com",
    "tool" : "pro"
  },
  "response" : {
    "domain_count" : "13",
    "domains" : [{
      "last_resolved" : "2012-01-11",
      "name" : "test1.com"
    },{
      "last_resolved" : "2012-01-11",
      "name" : "test2.com"
    }]
   }
 }

Upvotes: 0

Vigrond
Vigrond

Reputation: 8198

foreach ( $json_output->query->response->domains as $domain )
{
    echo $domain->name;
}

Study this http://json.org/

Upvotes: 1

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324820

query->response->domains is an indexed array, so you need to get an index, say [0], and then get the ->name from that.

echo $stf->response->domains[0]->name."\n";

Upvotes: 3

Related Questions