为了账号安全,请及时绑定邮箱和手机立即绑定

我将如何解码此 JSON 以插入 json 列?ErrorException:

我将如何解码此 JSON 以插入 json 列?ErrorException:

PHP
陪伴而非守候 2022-10-09 17:31:51
解码这个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 将请求转换为一个。


查看完整回答
反对 回复 2022-10-09
  • 1 回答
  • 0 关注
  • 133 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号