解碼這個json的正確方法是什么?我的模型有 protected $casts = [ 'items' => 'array' ];我的 json 項目:{ "data": [ { "name": "Google", "link": "http://google.com" }, { "name": "ALink", "link": "http://link.org" } ]}json_decode($request->items)返回錯誤:ErrorException: Object of class stdClass could not be converted to string in file
1 回答

HUWWW
TA貢獻1874條經驗 獲得超12個贊
你的 json 是一個對象,而不是一個數組。你應該把它轉換成這樣的對象
protected $casts = [
'items' => 'object'
];
要將 json 保存到您的模型中,您可以$obj->items = $request->items在如上所述將強制轉換更改為對象后執行此操作。
或者,如果您必須將其保存為數組,則可以通過執行(array)$request->items而不是 json_decode 將請求轉換為一個。
- 1 回答
- 0 關注
- 114 瀏覽
添加回答
舉報
0/150
提交
取消