>  기사  >  백엔드 개발  >  json_decode 问题

json_decode 问题

WBOY
WBOY원래의
2016-06-23 13:56:29978검색

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


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


但是 反过来就出问题了 

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


为什么 怎么才能变成

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


回复讨论(解决方案)

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 。 

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 。 


++

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

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

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

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


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

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


谢谢,如果是存数据库或者写缓存文件,不涉及不同语言交互呢?
성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.