我正在使用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"
你爲什麼要做'json_decode(json_encode($ response),true);'?什麼是編碼,然後立即解碼?什麼是'$ response'?爲什麼你不能直接使用它?你想用這條線解決什麼問題? –
P.S. '$ array1'不是一個對象,它是一個數組(即''json_decode()'的第二個參數)。 'echo'array1 ['operation'] ['result'] ['status']' –
我設法通過執行以下操作來解決問題: $ response = Requests :: get($ mydetails); $ array = json_decode($ response - > body); echo $ array-> operation-> result-> status; – djh123