为什么这不起作用?:将 json 解码为 php 数组

发布于 2024-11-07 17:53:26 字数 575 浏览 0 评论 0原文

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

该文件对一个数组进行编码,然后像这样回显它(并且 echo $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);

这不输出任何内容,没有错误,什么也没有!

$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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

盗琴音 2024-11-14 17:53:26

尝试这样做(您正在混合 "' [单引号而不是字符串上的双引号]):

$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);

结果:

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
                )

        )

)

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
                )

        )

)
梦纸 2024-11-14 17:53:26

尝试将 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}}}';

Try wrapping the json string in single quotes instead of double quotes:

$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}}}';
怼怹恏 2024-11-14 17:53:26

我执行以下代码没有任何问题:

$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);

我的猜测是您尝试读取的文件不存在。请记住,如果您使用的是 Linux,文件名区分大小写。使用 file_exists() 函数来检查:

var_dump(file_exists('输出json.php'));

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'));

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文