从json解码响应中获取变量值

时间:2022-10-09 00:28:00

I have a json response which is decode into an array $data as

我有一个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 ) )

I want to store [deliveryStatus] => Submitted this "Submitted" into a variable.

我想存储[deliveryStatus] =>将此“已提交”提交到变量中。

I have tried $dStatus=$data['deliveryStatus']; but its not working :(

我试过$ dStatus = $ data ['deliveryStatus'];但它无法正常工作:(

UPDATE

I tried to convert it to associative array by json_decode($data,TRUE);

我试图通过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 ) )

I got this.Now how to get the deliveryStatus variable?

我知道了。现在如何获得deliveryStatus变量?

3 个解决方案

#1


0  

If you want to access it a an associative array, you should convert it like an associative array first. Pass TRUE as a second argument to json_decode function as described in docs: http://php.net/json_decode

如果要访问关联数组,首先应将其转换为关联数组。传递TRUE作为json_decode函数的第二个参数,如docs中所述:http://php.net/json_decode

#2


0  

It's because you're accessing the data in the wrong fashion. json_decode returns an object, so you need to access these fields as object properties. For example:

这是因为您以错误的方式访问数据。 json_decode返回一个对象,因此您需要将这些字段作为对象属性进行访问。例如:

Instead of

$dStatus=$data['deliveryStatus'];

Try a member access format

尝试成员访问格式

$dStatus=$data->deliveryStatus;

If you want to access the data as an associated array, that's also quite simple.

如果要将数据作为关联数组访问,那也很简单。

When you call json_decode, pass true as the second parameter:

当你调用json_decode时,传递true作为第二个参数:

$myJson = json_decode($data,true);

Please refer to the document on json_decode for more information.

有关更多信息,请参阅json_decode上的文档。

#3


0  

I suggest to look at the view-source of the HTML you are outputting, or to wrap the print_r in a <pre></pre> tag, so that you can see the structure more easily.

我建议查看您输出的HTML的视图源,或者将print_r包装在

 
 
 标记中,以便您可以更轻松地查看结构。

Also, the elements are of class Object, which means they are not an array, so you need to use -> to access the elements of your objects.

此外,元素是Object类,这意味着它们不是数组,因此您需要使用 - >来访问对象的元素。

So if it is an object:

所以,如果它是一个对象:

$data = json_decode($response);
$dStatus = $data->outboundSMSMessageRequest->deliveryInfoList->deliveryInfo->deliveryStatus;

If it is an array, then:

如果是数组,那么:

$data = json_decode($response, true);
$dStatus = $data['outboundSMSMessageRequest']['deliveryInfoList']['deliveryInfo']['deliveryStatus'];

You see, the deliveryStatus entry is nested in sub-objects in the first case, and in sub-arrays in the second case.

您可以看到,deliveryStatus条目嵌套在第一种情况下的子对象中,而嵌套在第二种情况下的子数组中。

#1


0  

If you want to access it a an associative array, you should convert it like an associative array first. Pass TRUE as a second argument to json_decode function as described in docs: http://php.net/json_decode

如果要访问关联数组,首先应将其转换为关联数组。传递TRUE作为json_decode函数的第二个参数,如docs中所述:http://php.net/json_decode

#2


0  

It's because you're accessing the data in the wrong fashion. json_decode returns an object, so you need to access these fields as object properties. For example:

这是因为您以错误的方式访问数据。 json_decode返回一个对象,因此您需要将这些字段作为对象属性进行访问。例如:

Instead of

$dStatus=$data['deliveryStatus'];

Try a member access format

尝试成员访问格式

$dStatus=$data->deliveryStatus;

If you want to access the data as an associated array, that's also quite simple.

如果要将数据作为关联数组访问,那也很简单。

When you call json_decode, pass true as the second parameter:

当你调用json_decode时,传递true作为第二个参数:

$myJson = json_decode($data,true);

Please refer to the document on json_decode for more information.

有关更多信息,请参阅json_decode上的文档。

#3


0  

I suggest to look at the view-source of the HTML you are outputting, or to wrap the print_r in a <pre></pre> tag, so that you can see the structure more easily.

我建议查看您输出的HTML的视图源,或者将print_r包装在

 
 
 标记中,以便您可以更轻松地查看结构。

Also, the elements are of class Object, which means they are not an array, so you need to use -> to access the elements of your objects.

此外,元素是Object类,这意味着它们不是数组,因此您需要使用 - >来访问对象的元素。

So if it is an object:

所以,如果它是一个对象:

$data = json_decode($response);
$dStatus = $data->outboundSMSMessageRequest->deliveryInfoList->deliveryInfo->deliveryStatus;

If it is an array, then:

如果是数组,那么:

$data = json_decode($response, true);
$dStatus = $data['outboundSMSMessageRequest']['deliveryInfoList']['deliveryInfo']['deliveryStatus'];

You see, the deliveryStatus entry is nested in sub-objects in the first case, and in sub-arrays in the second case.

您可以看到,deliveryStatus条目嵌套在第一种情况下的子对象中,而嵌套在第二种情况下的子数组中。