繁体   English   中英

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

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

我有以下来自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\"}"
}

本质上,我需要访问数组的“消息”部分,并能够使用“ notificationType = Delivery”之类的值对。

我尝试使用PHP foreach外观遍历整个数组,并尝试按以下方式解码数组:

$message_data = json_decode($message,true);

但是我仍然在努力访问其中的数据。 注意:我的数据在变量$ message中。

关于如何访问消息数据的任何建议?

还希望访问“消息”部分中的部分,例如:

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

www.singles.dating

谢谢

原因是Message是序列化的json。

所以也需要解码

$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);
}

JSON的message部分本身就是JSON字符串,因此在解码整个JSON对象之后,您将需要再次解码message属性。 假设JSON位于名为$aws_notification的变量中,则可以执行以下操作:

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

然后使用可以访问$message_data[ 'notificationType' ]及其其他属性。

暂无
暂无

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

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