为什么这不起作用?:将 json 解码为 php 数组
$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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试这样做(您正在混合
"
和'
[单引号而不是字符串上的双引号]):结果:
Try this instead (you are mixing
"
and'
[single quotes instead of double quotes on the string]):And your result:
尝试将 json 字符串用单引号而不是双引号括起来:
Try wrapping the json string in single quotes instead of double quotes:
我执行以下代码没有任何问题:
我的猜测是您尝试读取的文件不存在。请记住,如果您使用的是 Linux,文件名区分大小写。使用
file_exists()
函数来检查:var_dump(file_exists('输出json.php'));
I had no problems executing the following code:
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'));