[英]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.