简体   繁体   English

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

[英]How to get the value for the given json and get result

I am trying to get the result for the below query 我正在尝试获取以下查询的结果

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

and while i do 而当我做

return $users; 返回$ users;

Here is my json 这是我的json

[{"Res":0}]

When i try to decode it, it shows me the error 当我尝试对其进行decode时,它显示了错误

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

When i var_dump i am getting as 当我var_dump我得到

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

So, How can i get the result of the 'Res' ? 那么,如何获得'Res'的结果?

The solution is in the error: 解决方案是在错误中:

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

That is, the result data is being returned as an array which based on your var_dump also contains your result object and subsequent data. 也就是说,结果数据将作为数组返回,该数组基于您的var_dump还包含结果对象和后续数据。

This should do it: 应该这样做:

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

Note that this is essentially just turning your JSON string into an object. 请注意,这实际上只是将您的JSON字符串变成一个对象。 You can use the second parameter to have it returned as an array if preferred: 如果需要,可以使用第二个参数将其作为数组返回:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM