2016-12-09 28 views
-1

在這裏,我叫一個API,因此他們將返回JSON對象,現在我想的JSON轉換成數組一些迴應,所以我試圖像json_decode( $響應,真正的);但我得到的錯誤如何解決JSON對象這一問題納入PHP數組


注意:數組字符串轉換在C:\ XAMPP \ htdocs中\他\ 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:\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

什麼聲明是「第16行」?你在哪裏嘗試'json_decode()'? – cFreed

+0

使用var_dump打印解碼數組,而不是回顯。另外,下次發佈實際上會產生錯誤的代碼。 – Sammitch

回答

1

現在,你已經表現出確切的代碼,這是很明顯!

當你寫echo json_decode($response,true);

  • 第一json_decode($response,true)執行你想要的東西,即返回數組
  • 然後嘗試echo這個數組,因此通知: 「Array對字符串的轉換」(因爲echo需要一個字符串)。