简体   繁体   中英

Why doesn't this work?: decoding json into php array

$json = file_get_contents('outputsjson.php');

The file encodes an array then just echoes it as this (and echo $json outputs this):

{"theList":{"1":{"name":"DSC04156.JPG","title":"DSC04156.JPG","width":3264},"2":{"name":"DSC04157.JPG","title":"DSC04157.JPG","width":3264},"3":{"name":"DSC04158.JPG","title":"DSC04158.JPG","width":3264},"4":{"name":"DSC04159.JPG","title":"DSC04159.JPG","width":3264}}} 

Now I'm trying to decode it from another page like this:

$myarray = json_decode($json, true);

print_r($myarray);

This outputs nothing, no errors, nothing!

Try this instead (you are mixing " and ' [single quotes instead of double quotes on the string]):

$json = '{"theList":{"1":{"name":"DSC04156.JPG","title":"DSC04156.JPG","width":3264},"2":{"name":"DSC04157.JPG","title":"DSC04157.JPG","width":3264},"3":{"name":"DSC04158.JPG","title":"DSC04158.JPG","width":3264},"4":{"name":"DSC04159.JPG","title":"DSC04159.JPG","width":3264}}} ';

$myarray = json_decode($json, true);

print_r($myarray);

And your result:

Array
(
    [theList] => Array
        (
            [1] => Array
                (
                    [name] => DSC04156.JPG
                    [title] => DSC04156.JPG
                    [width] => 3264
                )

            [2] => Array
                (
                    [name] => DSC04157.JPG
                    [title] => DSC04157.JPG
                    [width] => 3264
                )

            [3] => Array
                (
                    [name] => DSC04158.JPG
                    [title] => DSC04158.JPG
                    [width] => 3264
                )

            [4] => Array
                (
                    [name] => DSC04159.JPG
                    [title] => DSC04159.JPG
                    [width] => 3264
                )

        )

)

尝试将json字符串用单引号而不是双引号引起来:

$json = '{"theList":{"1":{"name":"DSC04156.JPG","title":"DSC04156.JPG","width":3264},"2":{"name":"DSC04157.JPG","title":"DSC04157.JPG","width":3264},"3":{"name":"DSC04158.JPG","title":"DSC04158.JPG","width":3264},"4":{"name":"DSC04159.JPG","title":"DSC04159.JPG","width":3264}}}';

I had no problems executing the following code:

$json = '{"theList":{"1":{"name":"DSC04156.JPG","title":"DSC04156.JPG","width":3264},"2":{"name":"DSC04157.JPG","title":"DSC04157.JPG","width":3264},"3":{"name":"DSC04158.JPG","title":"DSC04158.JPG","width":3264},"4":{"name":"DSC04159.JPG","title":"DSC04159.JPG","width":3264}}}';
$myarray = json_decode($json, true);
print_r($myarray);

My guess would be that the file you are trying to read from does not exist. Remember that, if you are using Linux, file names are case-sensitive. Use the file_exists() function to check this:

var_dump(file_exists('outputsjson.php'));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM