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

解码这个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


陪伴而非守候
浏览 134回答 1
1回答

HUWWW

你的 json 是一个对象,而不是一个数组。你应该把它转换成这样的对象protected $casts = [   'items' => 'object'];要将 json 保存到您的模型中,您可以$obj->items = $request->items在如上所述将强制转换更改为对象后执行此操作。或者,如果您必须将其保存为数组,则可以通过执行(array)$request->items而不是 json_decode 将请求转换为一个。
打开App,查看更多内容
随时随地看视频慕课网APP