Home > Backend Development > PHP Tutorial > json_decode 问题

json_decode 问题

WBOY
Release: 2016-06-23 13:56:29
Original
1027 people have browsed it

$bb = array(  array("id"=>"1","time"=>"5"),  array("id"=>"2","time"=>"50"),  array("id"=>"3","time"=>"555"),); print_r(json_encode($ew));
Copy after login


输出的是
[{"id":"1","time":"5"},{"id":"2","time":"50"},{"id":"3","time":"555"}]
Copy after login


但是 反过来就出问题了

$ew='[{"id":"1","time":"5"},{"id":"2","time":"50"},{"id":"3","time":"555"}]';
print_r(json_decode($ew));

变成

Array ( [0] => stdClass Object ( [id] => 1 [time] => 5 ) [1] => stdClass Object ( [id] => 2 [time] => 50 ) [2] => stdClass Object ( [id] => 3 [time] => 555 ) ) 
Copy after login


为什么 怎么才能变成

$bb = array(  array("id"=>"1","time"=>"5"),  array("id"=>"2","time"=>"50"),  array("id"=>"3","time"=>"555"),);
Copy after login


回复讨论(解决方案)

print_r(json_decode($ew, true));

手册总是要看的

json_decode ? 对 JSON 格式的字符串进行编码说明mixed json_decode ( string $json [, bool $assoc ] )接受一个 JSON 格式的字符串并且把它转换为 PHP 变量 参数json 待解码的 json string 格式的字符串。 assoc 当该参数为 TRUE 时,将返回 array 而非 object 。 
Copy after login
Copy after login

json_decode默认是object,要转为数组,第二个参数需要设为true。

print_r(json_decode($ew,true));

手册总是要看的

json_decode ? 对 JSON 格式的字符串进行编码说明mixed json_decode ( string $json [, bool $assoc ] )接受一个 JSON 格式的字符串并且把它转换为 PHP 变量 参数json 待解码的 json string 格式的字符串。 assoc 当该参数为 TRUE 时,将返回 array 而非 object 。 
Copy after login
Copy after login


++

请问当对象或数组需要字符串化保存的时候,json和序列化各有什么优势?

楼主,这个属于正常情况好不

json基本各个语言之间都能用

请问当对象或数组需要字符串化保存的时候,json和序列化各有什么优势?


json 用于外部交流
序列化 用于内部交流

json 用于外部交流
序列化 用于内部交流


谢谢,如果是存数据库或者写缓存文件,不涉及不同语言交互呢?
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template