2014-02-18 69 views
0

我正在使用PHP向另一個Web服務器發出HTTP請求以檢索JSON數據,該請求似乎可行,並且我可以使用var_dump查看數據,但我不確定我可以訪問數據。已成功獲取JSON PHP數據,但無法訪問它

這是我獲得的數據代碼:

$response = Requests::get($mydetails); 

$array1 = json_decode(json_encode($response), true); 

幾乎所有的我都試過的東西(如回聲$ array1->操作 - > result->狀態)給我同樣的錯誤信息:

PHP Notice: Trying to get property of non-object 

這是的var_dump輸出的前幾行:

array(9) { 
    ["body"]=> 
    string(7017) "{"operation":{"result":{"status":"Success","message":"Request details listed successfully"},"details":[{"fields":[{"name":"requesttype","value":"Incident","type":"Pick List","displayvalue":"Request Type","allowedvalues":["Incident","Request for Change","Request For Information","Request for Work"],"mandatory":"false"},{"name":"impact","value":"Affects User","type":"Pick List","displayvalue":"Impact","allowedvalues":["Affects Business","Affects Department","Affects Group","Affects User"],"mandatory":"false"},{"name":"status","value":"Closed","type":"Pick List","displayvalue":"Status","allowedvalues":["Awaiting Customer","Awaiting Third Party","Closed","More Information Required","Open","Resolved"],"mandatory":"false"},{"name":"impactdetails","value":"-","type":"Multi Line","displayvalue":"Impact Details","allowedvalues":[""],"mandatory":"false"},{"name":"mode","value":"Phone Call","type":"Pick List","displayvalue":"Mode","allowedvalues":["Customer Portal","E-Mail","Fax","On-Site Request","Phone Call"],"mandatory":"false"},{"name":"priority","value":"Severity 2","type":"Pick List","displayvalue":"Priority","allowedvalues":["National Level 1","National Level 2","Request for Change","Severity 1","Severity 2","Severity 3","Severity 4"],"mandatory":"false"},{"name":"department","value":"General Practice","type":"Not Editable","displayvalue":"Department","allowedvalues":[""],"mandatory":"true"},{"name":"requesteremail","value":"","type":"Not Editable","displayvalue":"E-mail","allowedvalues":[""],"mandatory":"false"},{"name":"requestercontactnumber","value":"","type":"Not Editable","displayvalue" 
+1

你爲什麼要做'json_decode(json_encode($ response),true);'?什麼是編碼,然後立即解碼?什麼是'$ response'?爲什麼你不能直接使用它?你想用這條線解決什麼問題? –

+0

P.S. '$ array1'不是一個對象,它是一個數組(即''json_decode()'的第二個參數)。 'echo'array1 ['operation'] ['result'] ['status']' –

+0

我設法通過執行以下操作來解決問題: $ response = Requests :: get($ mydetails); $ array = json_decode($ response - > body); echo $ array-> operation-> result-> status; – djh123

回答

1

的DECOD後的結構ing是一個array,所以你應該使用像$array1['body']['operation']['result']['status']這樣的數組運算符來訪問它,而不是->--它是對象遍歷運算符。你也可以將它解碼作爲一個對象:

$response = Requests::get($mydetails); 

$array1 = json_decode(json_encode($response)); 

但你的反應似乎已經是一個array,所以只是訪問它,而無需重新編碼,也許?

編輯: 後做你的代碼中的一些假設,你也許應該這樣做:

$response = Requests::get($mydetails); 

$array1 = json_decode($response['body'], true); 
var_dump($array1); // looks ok? 
+0

感謝您的回覆,我刪除了'true'選項,並將我的代碼更改爲以下內容,但仍然無法使用echo $ jsondata-> operation-> result-> status訪問數據:: $ response =請求::得到($ myMANAGEENGINEAddressfull); $ array = json_decode(json_encode($ response)); $ jsondata = $ array-> body; – djh123

+0

你能解釋一下你爲什麼要做'json_decode(json_encode($ response))'?這將從PHP數組(json_encode)創建一個JSON字符串,然後將從剛剛編碼的JSON字符串('json_decode')創建相同的數組。這不是你應該做的!你可以在獲得它之後立即顯示'var_dump($ response)',而不需要解碼或編碼任何東西? – ddinchev

0

如果使用true屬性爲JSON解碼你有你自己的JSON關聯數組所以你會需要訪問示波器

$array1['operation']['result']['status']; 
+0

謝謝您的回覆,但是當我嘗試給出錯誤「PHP Notice:Undefined index:operation」 – djh123