如何在php中解码从curl产生的json文件

Posted

技术标签:

【中文标题】如何在php中解码从curl产生的json文件【英文标题】:How to decode json file produced from curl in php 【发布时间】:2020-06-24 15:28:05 【问题描述】:

在我的curl结束时,如下

  $response = curl_exec($curl);
  $err = curl_error($curl);
  
  curl_close($curl);
  
  if ($err) 
    echo "cURL Error #:" . $err;
   else 
    echo $response;
  

我明白了

 "status": true, "message": "Account number resolved", "data":  "account_number": "xxxxxxx", "account_name": "ONY xxxx xxxx", "bank_id": 21  

现在,我正在尝试解码

现在我正在尝试解码 $responds 并在尝试此操作时获取 account_name

var_dump($response);
$var = json_decode($response);

echo $data->account_number

但是没有用,请帮忙。

【问题讨论】:

【参考方案1】:

您需要在使用$data->account_number之前引用$var

<?php

$response= '"status":true,"message":"Account number resolved","data":"account_number":"xxxxxxx","account_name":"ONY xxxx xxxx","bank_id":21';

$var = json_decode($response);

echo($var->data->account_number); // prints xxxxxxx

【讨论】:

以上是关于如何在php中解码从curl产生的json文件的主要内容,如果未能解决你的问题,请参考以下文章