如何获取给定 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");

当我这样做时

return $用户;

这是我的 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);