我使用PHP向另一个Web服务器发出HTTP请求以检索JSON数据,该请求似乎有效,我可以使用var_dump查看数据,但我不确定我是否可以访问数据。
这是我获取数据的代码:
$response = Requests::get($mydetails);
$array1 = json_decode(json_encode($response), true);
我尝试的几乎所有内容(例如echo $ array1-> operation-> result-> status)都给出了相同的错误消息:
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"
答案 0 :(得分:1)
解码后的结构是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?
答案 1 :(得分:0)
如果对json解码使用true
属性,则将json置于关联数组中,因此您需要使用范围访问
$array1['operation']['result']['status'];