PHP中 json_decode json数据后foreach出现“Cannot use object of type stdClass as array in XXX”错误及其解决方法
答案:
加上True即可!
结果:
stdClass Object
(
[code] => A00006
[data] => stdClass Object
(
[ uid] => stdClass Object
(
[relation] => 0
[gid] => 11
[stat] =>
)
)
)
加上True后:
Array
(
[code] => A00006
[data] => Array
(
[ uid] => Array
(
[relation] => 0
[gid] => 11
[stat] =>
)
)
)
参看:http://www.maycode.com/index.php/hotspot/32-web20/587-json.html
$json2array = json_decode($json,TRUE);
加上True即可!
<?php
$json = "{\"code\":\"A00006\",
\"data\":
{
\" uid\":
{
\"relation\":\"0\",
\"gid\": \"11\",
\"stat\" : \"\"
}
}
}";
$json2array = json_decode($json);
//$json2array = json_decode($json,TRUE);//这样foreach就没有该问题了
print_r($json2array);
?>
$json = "{\"code\":\"A00006\",
\"data\":
{
\" uid\":
{
\"relation\":\"0\",
\"gid\": \"11\",
\"stat\" : \"\"
}
}
}";
$json2array = json_decode($json);
//$json2array = json_decode($json,TRUE);//这样foreach就没有该问题了
print_r($json2array);
?>
结果:
stdClass Object
(
[code] => A00006
[data] => stdClass Object
(
[ uid] => stdClass Object
(
[relation] => 0
[gid] => 11
[stat] =>
)
)
)
加上True后:
Array
(
[code] => A00006
[data] => Array
(
[ uid] => Array
(
[relation] => 0
[gid] => 11
[stat] =>
)
)
)
参看:http://www.maycode.com/index.php/hotspot/32-web20/587-json.html
作者:jackxiang@向东博客 专注WEB应用 构架之美 --- 构架之美,在于尽态极妍 | 应用之美,在于药到病除
地址:https://jackxiang.com/post/1595/
版权所有。转载时必须以链接形式注明作者和原始出处及本声明!
最后编辑: jackxiang 编辑于2009-1-13 18:59
评论列表