如何在json对象中将此问题解决为php数组

时间:2016-12-09 18:11:18

标签: php json

这里我调用一个API,所以他们会在 json对象中返回一些响应,现在我想将 json转换为数组,所以我想尝试 json_decode ($ response,true); 但我收到错误

注意:第16行的C:\ xampp \ htdocs \ he \ rest.php中的数组到字符串转换 阵列

我不知道如何解决此错误,

   <?php 
  $request_url = 'http://www.domain.com/api/method/properties';
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $request_url);
  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  $response = curl_exec($ch);
  curl_close ($ch);
   echo "<pre>";
  print_r($response);// Here we can get the respoonse in 
  echo "</pre>"; 
  echo json_decode($response,true)//Notice: Array to string conversion in        C:\xampp\htdocs\he\rest.php on line 16
Array
   ?>



//Resposnse coming like this
[ {
  "createdBy" : "agent",
  "createdDate" : "2016-09-20T08:13:30.418Z",
  "lastModifiedBy" : null,
  "lastModifiedDate" : "2016-10-12T09:10:20.847Z",
  "id" : "57e387cef1a8a02f7a0c9613",
  "name" : "2BHK Apartment in Sanjay Nagar",
  "contactName" : null,
  "propertyPurpose" : "RESIDENTIAL"
  },
  {
  "createdBy" : "agent",
  "createdDate" : "2016-09-20T08:13:30.418Z",
  "lastModifiedBy" : null,
  "lastModifiedDate" : "2016-09-20T08:13:30.539Z",
  "id" : "57e387e1f1a8a02f7a0c9616",
  "name" : "2BHK Apartment in Mira Road",
  "contactName" : null,
  "propertyPurpose" : "RESIDENTIAL",
  }
]

1 个答案:

答案 0 :(得分:1)

现在您已经显示了确切的代码,这很明显!

撰写echo json_decode($response,true);时:

  • 首先json_decode($response,true)执行您想要的操作,即返回数组
  • 然后你尝试echo这个数组,因此通知:“数组转换为字符串”(因为echo需要一个字符串)。