首頁  >  文章  >  後端開發  >  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