简体   繁体   English

PHP-JSON数组-不确定如何提取数据

[英]PHP - JSON Array - Unsure how to extract data

I have the following JSON/Array from AWS and I'm struggling to read it with PHP: 我有以下来自AWS的JSON / Array,但正在努力用PHP读取它:

{
  "Type" : "Notification",
  "MessageId" : "666483cb-e012-51f2-8d66-d308d55efd98",
  "TopicArn" : "arn:aws:sns:us-east-1:848283244672:S-Notification-Queue",
  "Message" : "{\"notificationType\":\"Delivery\",\"mail\"}"
}

Essentially I need to access the "Message" part of the array and be able to use the value pairs within like "notificationType = Delivery". 本质上,我需要访问数组的“消息”部分,并能够使用“ notificationType = Delivery”之类的值对。

I've tried looping through the array with a PHP foreach look and I've tried decoding the array as follows: 我尝试使用PHP foreach外观遍历整个数组,并尝试按以下方式解码数组:

$message_data = json_decode($message,true);

however I'm still struggling to access the data within. 但是我仍然在努力访问其中的数据。 Note: I have the data in a variable $message. 注意:我的数据在变量$ message中。

Any advice on how to access the message data? 关于如何访问消息数据的任何建议?

Also hoping to access parts within the Message section like: 还希望访问“消息”部分中的部分,例如:

{\"name\":\"Subject\",\"value\":\"abc"}

www.singles.dating www.singles.dating

thankyou 谢谢

The reason is that Message is serialized json. 原因是Message是序列化的json。

So it needs decoding also 所以也需要解码

$message_data = json_decode($message, true);
if(
  isset($message_data['Message']) AND !is_array($message_data['Message'])
) {
  $message_data['Message'] = json_decode($message_data['Message'], true);
}

The message section of that JSON is itself a string of JSON, so after decoding the whole JSON object you will need to decode the message property again. JSON的message部分本身就是JSON字符串,因此在解码整个JSON对象之后,您将需要再次解码message属性。 Assuming that JSON is in a variable called $aws_notification , you can do this: 假设JSON位于名为$aws_notification的变量中,则可以执行以下操作:

$message_json = json_decode( $aws_notification, true )[ 'Message' ];
$message_data = json_decode( $message_json, true );

Then use can access $message_data[ 'notificationType' ] and its other properties. 然后使用可以访问$message_data[ 'notificationType' ]及其其他属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM