3

This is a bit of a noob question. I'm working with a JSON file which looks something like this:

{"nodes": 
  [{"node": 
     {"Vocabulary name":"Bestseller Format",
      "Term":"Hardcover Fiction",
      "Bestseller1":"9780470227800",
      "Bestseller2":"9781451617962",
      "Bestseller3":"9781439167397",
      "Bestseller4":"9781617750106",
      "Bestseller5":"9780385533300",
      "Bestseller6":"9780670022526",
      "Bestseller7":"9781609530358",
      "Bestseller8":"9780132358040",
      "Bestseller9":"9780596159771",
      "Bestseller10":"9780151014163",
      "Bestseller11":"9780393334807",
      "Bestseller12":"9780805090161"}
  }]
}

And I am trying to parse it in php. I have come up with a script

<?php       
  $jsonurl = "http://www.xtracrunchy.com/pandpnewlook/?q=pandp/bestsellers";
  $json = file_get_contents($jsonurl,NULL, NULL, 0, 1000);
  $json_output = json_decode($json,true);

  $bestseller_array = $json_output[nodes][0][node];

  foreach ($bestseller_array as $key => $value)
  {
     print $key;
     print " - ";
     print $value;
     print "<br />";
   }

?>

I would like to be able to eventually build an ordered list of Bestseller ISBN's but the only output I get when I run this script is

Vocabulary name - Bestseller Format Term - Hardcover Fiction

and when I add print count($bestseller_array); I only get 2 elements in the array.

Any help would be appreciated.

6
  • could you put code with code tags? and make everything readable... thanks :D Commented Apr 26, 2011 at 22:56
  • Fixed the formatting myself. Next time, add 4 spaces before each line of code. Commented Apr 26, 2011 at 22:59
  • what will show var_dump($bestseller_array); ? Commented Apr 26, 2011 at 23:03
  • try print_r($json_output) \n php.net/manual/en/function.print-r.php Commented Apr 26, 2011 at 23:03
  • when I do the print_r I get: Array ( [nodes] => Array ( [0] => Array ( [node] => Array ( [Vocabulary name] => Bestseller Format [Term] => Hardcover Fiction ) ) ) ) Vocabulary name - Bestseller Format Commented Apr 26, 2011 at 23:05

3 Answers 3

1

First of, I strongly advise you make use of CURL instead of file_get_contents(), because in some occasions, it won't work. But since it's not a part of the question, I won't talk about this any further.

Secondly, you're making use of unnamed literal constants, which is very bad.

$json_output[nodes][0][node];

Should be:

$json_output['nodes'][0]['node'];

See the documentation here on why.

Finally, the JSON string you wrote above doesn't match with what is returned on that url, and the script does work as expected on this "new" json.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. This was actually really helpful. It still isn't working but this sort of of points me in the right direction.
1

@flakes if you visit the URL you have in your $jsonurl you only get:

{"nodes":[{"node":{"Vocabulary name":"Bestseller Format","Term":"Hardcover Fiction"}}]}

Are you sure you're not missing something from the URL?

Comments

0

The output from the server doesn't match what you expect.

var_dump( $json );
//string(87) "{"nodes":[{"node":{"Vocabulary name":"Bestseller Format","Term":"Hardcover Fiction"}}]}"

Hope that helps...

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.