php再調用json_decode從字符串對象生成json對象時,若是使用[]操做符取數據,會獲得下面的錯誤
錯誤:
Cannot use object of type stdClass as array
產生緣由:
php
+展開json
-PHP數組
$res = json_decode($res);
$res['key']; //把 json_decode() 後的對象看成數組使用。spa
解決方法(2種):
一、使用 json_decode($d, true)。就是使json_decode 的第二個變量設置爲 true。
二、json_decode($res) 返回的是一個對象, 不能夠使用 $res['key'] 進行訪問, 換成 $res->key 就能夠了。
參考手冊:json_decode
Return Values:Returns an object or if the optional assoc parameter is TRUE, an associative array is instead returned.code
返回值默認是JSON對象,當第二個可選參數是TRUE的時候,則返回的是數組對象