doutu4335 2018-04-18 10:33
浏览 29
已采纳

在PHP中处理JSON响应

Using PHP, I have decoded this JSON Response..

Array ( [Request] => Array ( [RequestGuid] => 4ca35135-2560-42ef-bd63-6b22cbf369ed [PackageId] => c1bf0061-b0b7-4f71-8a9a-43d6e468e5ec [PackageVersion] => 2 [ResponseVersion] => 2 [DataKeys] => Array ( [Vrm] => KM14AKK ) ) [Response] => Array ( [StatusCode] => Success [StatusMessage] => Success [StatusInformation] => Array ( [Lookup] => Array ( [StatusCode] => Success [StatusMessage] => Success [AdviceTextList] => Array ( ) ) )

When I run...

  $data = json_decode($response, true);
  for ($i=0;$i<count($jsonArray);$i++){
    echo $jsonArray[$i];
  }

I get the error...

Notice: Undefined offset: 0

Any quick pointers as to how to access data in this feed, arrays always blag my head, I can see there is no key in this one?

Thanks.

  • 写回答

4条回答 默认 最新

  • duandanai6470 2018-04-18 10:40
    关注

    Use foreach loop instead of for loop so change loop as:

    $data = json_decode($response, true);
      for ($i=0;$i<count($jsonArray);$i++){
        echo $jsonArray[$i];
      }
    

    To:

    $data = json_decode($response, true);
    foreach($data as $d){
       print_r($d); 
    }
    

    Here your $response JSON data does not have index value. It has keys and values pair. So you can not use index here.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 MCNP里如何定义多个源?
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏