如何获取给定 json 的值并获取结果


How to get the value for the given json and get result

我正在尝试获取以下查询的结果

$users = DB::select("SELECT FIND_IN_SET('l','a,b,c,d') as Res");

当我这样做时

返回$users;

这是我的 json

[{"Res":0}]

当我尝试decode它时,它向我显示错误

json_decode() expects parameter 1 to be string, array given

当我var_dump我得到

array(1) { [0]=> object(stdClass)#773 (1) { ["Res"]=> int(0) } }

那么,我怎样才能得到'Res'的结果呢?

解决方案在错误中:

json_decode() expects parameter 1 to be string, **array given**

也就是说,结果数据作为数组返回,该数组基于您的var_dump还包含您的结果对象和后续数据。

这应该这样做:

<?php
     $data = $users[0]->Res
     $decoded = json_decode($data);

请注意,这实际上只是将 JSON 字符串转换为对象。如果需要,可以使用第二个参数将其作为数组返回:

<?php 
     $data = $users[0]->Res
     $decoded = json_decode($data, true);