php在調用json_decode從字符串對象生成json對象時,如果使用[]操作符取數據,會得到下面的錯誤
一、錯誤位置
報錯如圖所示:
報錯代碼
二、解決辦法
1、使用json_decode($res,true ) 。就是使jsond_ecode的第二個變量設置為true 。
$data = json_decode($data,true);
2、json_decode($res) 返回的是一個對象, 不可以使用 $res[‘key’] 進行訪問, 換成 $res->key 就可以了。
三、原因
$res = json_decode($res);
$res['key']; //把 json_decode() 后的對象當作數組使用。