Mihai Bujanca
Mihai Bujanca

Reputation: 4219

var_dump returns null, can't figure out why

   <?php
     $json_string = file_get_contents('infile.json');
     fwrite($fileout, $json_string);
     var_dump(json_decode($json_string));
     $get_json_values=json_decode($json_string,true);
        if(is_array($get_json_values))
        {
        foreach ($get_json_values as $getlikes) {  ?>
          <li>
            <a href="https://www.facebook.com/<?php echo $getlikes['id']; ?>" target="_top">
          </li>
        <?php
      } }
       ?>

I am trying to get values from a json file and I just can't figure out why var_dump returns NULL. Also, if I remove the if, for obvious reasons, there is a
Warning: Invalid argument supplied for foreach()

Please help me with this, i've been working on this the whole day and it's just so frustrating that it doesn't work out in any way.

Note: The name of the file is correct, the file is in the same directory with the .php file, there is data in infile.json and it is printed when fwrite($fileout, $json_string); is done.

What would be the problem, and what should I do?


Edit: The json file has about 1000 entries containing facebook page data. They all have the following structure:

{"category":"Musician\/band","name":"Yann Tiersen (official)","id":"18359161762","created_time":"2013-03-09T23:00:54+0000"}

var_dump($json_string) prints the data from the file.

if this helps in any way, the json file can be downloaded here


I have made up my json like this:

function idx(array $array, $key, $default = null) {
  return array_key_exists($key, $array) ? $array[$key] : $default;
}
$likes = idx($facebook->api('/me/likes'), 'data', array());
foreach ($likes as $like) {

                    fwrite($fileout,json_encode($like));
                    fwrite($fileout,PHP_EOL );
                  ?>`

So, what should I change would be:
1) add fwrite($fileout, "["); before and fwrite($fileout,"]"); after foreach.
2) add fwrite($fileout,",") before fwrite($fileout,PHP_EOL ); Is this right?

Upvotes: 0

Views: 5302

Answers (2)

Sven
Sven

Reputation: 70893

Your JSON file IS invalid.

Basically you have plenty of lines that are all valid JSON in itself, but they are not valid combined!

Invalid:

{"category":"Musician\/band","name":"Yann Tiersen (official)","id":"18359161762","created_time":"2013-03-09T23:00:54+0000"}
{"category":"Travel\/leisure","name":"Me hitchhiking around the world","id":"221159531328223","created_time":"2013-03-09T13:24:06+0000"}

Would be valid:

[
    {"category":"Musician\/band","name":"Yann Tiersen (official)","id":"18359161762","created_time":"2013-03-09T23:00:54+0000"}
,
    {"category":"Travel\/leisure","name":"Me hitchhiking around the world","id":"221159531328223","created_time":"2013-03-09T13:24:06+0000"}
]

You can either decode the JSON line by line, or convert it into a valid format.

Upvotes: 2

SeanCannon
SeanCannon

Reputation: 77996

You likely have invalid JSON in infile.json. Per the docs of json_decode():

NULL is returned if the json cannot be decoded or if the encoded data is deeper 
than the recursion limit.

Upvotes: 2

Related Questions