这是我的代码:
$array = array(
"Birds" =>
array(
'group' => 1,
"Bird" => array(
array('id' => 1, 'img' => "img1.png", 'title' => "kate"),
array('id' => 2, 'img2' => "img2.png", 'title' => "mary")
)) );
$json = json_encode($array);
echo json_decode($json);
输出是:
//JSON OUTPUT {"Birds":{"group":1,"Bird":[{"id":1,"img":"img1.png","title":"cardinal"},{"id":2,"img2":"img2.png","title":"bluejay"}]}}
类stdClass的对象无法转换为字符串
答案 0 :(得分:1)
试
var_dump($json);
这允许您打印出对象和其他非主要类型的详细信息。
Echoing用于字符串 - 您的json解码字符串将是stdClass
请参阅var_dump
http://php.net/manual/en/function.var-dump.php
答案 1 :(得分:0)
使用var_dump
查看变量echo将变量转换为字符串,而它是对象。你也可以向json_decode($data, true)
添加第二个参数来获取数组而不是对象,因为我认为这是你想要的,因为输入是数组。
关于将对象转换为字符串,您可以阅读 __toString
答案 2 :(得分:0)
您必须将json_decode的第二个参数传递为true,有关这些参数的更多信息,请访问:http://php.net/manual/en/function.json-decode.php
因此您的echo json_decode($json);
应更改为:
print_r(json_decode($json, true));
echo
更改为print_r
,因为输出的数组不是字符串...