解码这个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 关注
- 133 浏览
添加回答
举报
0/150
提交
取消