doxd96148 2015-03-12 01:34
浏览 90
已采纳

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

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;

Here is my json

[{"Res":0}]

When i try to decode it, it shows me the error

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

When i var_dump i am getting as

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

So, How can i get the result of the 'Res' ?

  • 写回答

1条回答 默认 最新

  • dongzong7467 2015-03-12 02:21
    关注

    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.

    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. 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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题