如何将JSON对象中的此问题解析到PHP数组中



在这里我致电一个API,因此他们将返回 JSON对象中的一些响应我正在尝试像 JSON_DECODE($ wendesp,true); 一样,但是我会得到错误


注意:c: xampp htdocs he rest.php在第16行中的数组转换为字符串转换数组

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

   <?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:xampphtdocsherest.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",
  }
]

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

echo json_decode($response,true);

  • 第一个json_decode($response,true)执行您想要的内容,即返回 array
  • 然后,您尝试使用echo此数组,因此通知:"到字符串转换"(因为echo期望字符串)。

最新更新