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 求帮我调试一下freefem代码
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图