dongwei8729 2015-11-20 17:58
浏览 30
已采纳

无法打印简单的JSON?

I know it's basic, but I couldn't get what's wrong because it's not my area. I am sending some request to a server and print the response like this:

$rest = curl_init();  
 curl_setopt($rest,CURLOPT_URL,$url);  
 curl_setopt($rest,CURLOPT_GET,1);  
 curl_setopt($rest,CURLOPT_HTTPHEADER,$headers);  
 curl_setopt($rest,CURLOPT_SSL_VERIFYPEER, false);  
 curl_setopt($rest,CURLOPT_RETURNTRANSFER, true);  
 $response = curl_exec($rest);  
 $json = json_decode($response, true);

 echo $response;

Where I get this:

{"results":[{"Devices":["52401E7E-C5D7","AE80C0F8-999E","764BFD92-9753","78A23379-2C14","EEA03545-5EB9","E18DDFEC-C3C9"],"UserID":"433011AC-228A-4931-8700-4D050FA18FC1","createdAt":"2015-11-04T15:06:33.564Z","objectId":"3os7BGxRoG","updatedAt":"2015-11-04T17:08:57.635Z"}]}

Then, I am trying to print the JSON or its fields, but I then get nothing:

  echo $json;
  echo $json['UserID'];
  echo $json['Devices'];
  • 写回答

1条回答 默认 最新

  • dongxi5494 2015-11-20 18:48
    关注

    To clarify the comments a bit:

    $str = '{"results":[{"Devices":["52401E7E-C5D7","AE80C0F8-999E","764BFD92-9753","78A23379-2C14","EEA03545-5EB9","E18DDFEC-C3C9"],"UserID":"433011AC-228A-4931-8700-4D050FA18FC1","createdAt":"2015-11-04T15:06:33.564Z","objectId":"3os7BGxRoG","updatedAt":"2015-11-04T17:08:57.635Z"}]}';
    $json = json_decode($str, true); // to an associative array
    // to echo the UserID
    echo "userID : " . $json['results'][0]['UserID'];
    // output: userID : 433011AC-228A-4931-8700-4D050FA18FC1
    
    // to get the structure of the json array in general use print_r() as AbraCadaver pointed out
    print_r($json);
    

    In your attempt you were missing the results[0] part.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记