我有一个json响应,它被解析为数组$ data as
stdClass Object ( [outboundSMSMessageRequest] => stdClass Object ( [deliveryInfoList] => stdClass Object ( [deliveryInfo] => stdClass Object ( [address] => 8606142527 [deliveryStatus] => Submitted ) [resourceURL] => http://api-testmobile.com/smsmessaging/1/outbound/OPNHSE/requests/urn:uuid:0f55fd13-a419-4ad9-adec-3dcf63ca39c1/deliveryInfos ) [senderAddress] => OPNHSE [outboundSMSTextMessage] => stdClass Object ( [message] => Sam has requested a payment of Rs 10.00. ) [clientCorrelator] => [receiptRequest] => stdClass Object ( [notifyURL] => [callbackData] => ) [senderName] => [resourceURL] => http://api-openhouse.testingmobile.com/smsmessaging/1/outbound/OPNHSE/requests/urn:uuid:0f5-a419-4ad9-adec-3dcf63ca39c1 ) )
我想将[deliveryStatus] => Submitted
此“已提交”存储到变量中。
我已经尝试了$dStatus=$data['deliveryStatus'];
,但它不起作用:(
更新
我试图通过json_decode将它转换为关联数组($ data,TRUE);
Array ( [outboundSMSMessageRequest] => Array ( [deliveryInfoList] => Array ( [deliveryInfo] => Array ( [address] => 98989 [deliveryStatus] => Submitted ) [resourceURL] => http://api-otest.com/smsmessaging/1/outbound/OPNHSE/requests/urn:uuid:3b277b5b-cf79-4551-872f-16674499bc09/deliveryInfos ) [senderAddress] => OPNHSE [outboundSMSTextMessage] => Array ( [message] => sam has requested a payment of Rs 100.00 through payt.me . Kindly clickhttps://www.test.me/test to pay. ) [clientCorrelator] => [receiptRequest] => Array ( [notifyURL] => [callbackData] => ) [senderName] => [resourceURL] => http://api-test.com/smsmessaging/1/outbound/OPNHSE/requests/urn:uuid:3b277b5b-cf79-4551-872f-16674499bc09 ) )
我知道了。现在如何获得deliveryStatus变量?
答案 0 :(得分:0)
我建议您查看要输出的HTML的视图来源,或者将print_r
包装在<pre></pre>
标记中,以便您可以更轻松地查看结构。
此外,元素属于Object类,这意味着它们不是数组,因此您需要使用->
来访问对象的元素。
所以,如果它是一个对象:
$data = json_decode($response);
$dStatus = $data->outboundSMSMessageRequest->deliveryInfoList->deliveryInfo->deliveryStatus;
如果是数组,那么:
$data = json_decode($response, true);
$dStatus = $data['outboundSMSMessageRequest']['deliveryInfoList']['deliveryInfo']['deliveryStatus'];
您可以看到,deliveryStatus
条目嵌套在第一种情况下的子对象中,而嵌套在第二种情况下的子数组中。
答案 1 :(得分:0)
如果要访问关联数组,首先应将其转换为关联数组。将TRUE
作为json_decode
函数的第二个参数传递,如文档中所述:http://php.net/json_decode
答案 2 :(得分:0)
这是因为您以错误的方式访问数据。 json_decode返回一个对象,因此您需要将这些字段作为对象属性进行访问。例如:
而不是
$dStatus=$data['deliveryStatus'];
尝试会员访问格式
$dStatus=$data->deliveryStatus;
如果要将数据作为关联数组访问,那也很简单。
当您致电json_decode
时,请将true
作为第二个参数传递:
$myJson = json_decode($data,true);
有关详细信息,请参阅document on json_decode。