如何使用 PHP 抓取 JSON 中的嵌套字符串?
How to grab this nested string in JSON with PHP?
我正在遍历只是 JSON 输出的电子邮件正文。我正在尝试获取单个字符串 emailAddress
,但我不确定语法。我能够访问 JSON 的 Message
部分,但我尝试访问的任何其他内容,最终都会得到 Illegal string offset
.
所以,如果我的 JSON 是:
[Type] => Notification
[MessageId] => gibberishhere
[TopicArn] => arn:aws:somethingsomethingsomething
[Message] => {
"notificationType":"Bounce",
"bounce":{
"feedbackId":"blahblahblahblahblah",
"bounceType":"Permanent",
"bounceSubType":"General",
"bouncedRecipients":[{
"emailAddress":"bounce@simulator.amazonses.com",
"action":"failed",
"status":"5.1.1",
"diagnosticCode":"smtp; 550 5.1.1 user unknown"}],
"timestamp":"2020-11-02T16:37:13.000Z",
"remoteMtaIp":"ip.address.here",
"reportingMTA":"dsn; somethingsomething"},
"mail":{
"timestamp":"2020-11-02T16:37:13.029Z",
"source":"test@emailaddress.com",
"sourceArn":"arn:somethingsomethingdotcom",
"sourceIp":"ip.address.here",
"sendingAccountId":"somethingsomething",
"messageId":"numbersnumbersnumbers1234567890",
"destination":["bounce@simulator.amazonses.com"]
}
}
[Timestamp] => 2020-11-02T16:37:13.677Z
[SignatureVersion] => 1
[Signature] => blahblahblah
[SigningCertURL] => blahblahblah
[UnsubscribeURL] => blahblahblah
我用这个来解码它:
$message = json_decode($message, true);
echo $message['Message'];
我得到这个输出:
{
"notificationType":"Bounce",
"bounce":{
"feedbackId":"blahblahblahblahblah",
"bounceType":"Permanent",
"bounceSubType":"General",
"bouncedRecipients":[{
"emailAddress":"bounce@simulator.amazonses.com", <---- I NEED THIS FIELD
"action":"failed",
"status":"5.1.1",
"diagnosticCode":"smtp; 550 5.1.1 user unknown"}],
"timestamp":"2020-11-02T16:37:13.000Z",
"remoteMtaIp":"ip.address.here",
"reportingMTA":"dsn; e226-55.smtp-out.us-east-2.amazonses.com"},
"mail":{
"timestamp":"2020-11-02T16:37:13.029Z",
"source":"test@emailaddress.com",
"sourceArn":"arn:somethingsomethingdotcom",
"sourceIp":"ip.address.here",
"sendingAccountId":"somethingsomething",
"messageId":"numbersnumbers1234567890",
"destination":["bounce@simulator.amazonses.com"]
}
}
我只需要 emailAddress
字段。我需要走多远才能抓住它?我试过了
$message['Message']['bounce']
、$message['Message']['emailAddress']
和其他几个人,但他们都是 return Illegal string offset
.
使用关联数组相对容易,如评论中所建议:
$arr = json_decode($message['Message'], true);
现在您需要做的就是引用正确的元素,遵循路径:
echo $arr['bounce']['bouncedRecipients'][0]['emailAddress'];
给出:
bounce@simulator.amazonses.com
编辑:如何引用元素 emailAddress
- 解释 [0] 索引
为了更好地了解$arr
的数组结构,可以使用:
echo '<pre>';
print_r($arr);
echo '</pre>';
输出:
Array
(
[notificationType] => Bounce
[bounce] => Array
(
[feedbackId] => blahblahblahblahblah
[bounceType] => Permanent
[bounceSubType] => General
[bouncedRecipients] => Array
(
[0] => Array
(
[emailAddress] => bounce@simulator.amazonses.com
[action] => failed
[status] => 5.1.1
[diagnosticCode] => smtp; 550 5.1.1 user unknown
)
)
[timestamp] => 2020-11-02T16:37:13.000Z
[remoteMtaIp] => ip.address.here
[reportingMTA] => dsn; e226-55.smtp-out.us-east-2.amazonses.com
)
[mail] => Array
(
[timestamp] => 2020-11-02T16:37:13.029Z
[source] => test@emailaddress.com
[sourceArn] => arn:somethingsomethingdotcom
[sourceIp] => ip.address.here
[sendingAccountId] => somethingsomething
[messageId] => numbersnumbers1234567890
[destination] => Array
(
[0] => bounce@simulator.amazonses.com
)
)
)
现在您需要做的就是按照 'path' 找到您要引用的元素。 IE。对于 emailAddress
让我们反过来 'route':
emailAddress
存在于键为 [0]
的数组中(你有它,键 [0]
)
键 [0]
存在于键 [bouncedRecipients]
的数组中
键 [bouncedRecipients]
存在于键 [bounce]
的数组中
键 [bounce]
位于数组 $arr
.
的根目录中
至此,元素的路径:
['bounce']['bouncedRecipients'][0]['emailAddress']
我正在遍历只是 JSON 输出的电子邮件正文。我正在尝试获取单个字符串 emailAddress
,但我不确定语法。我能够访问 JSON 的 Message
部分,但我尝试访问的任何其他内容,最终都会得到 Illegal string offset
.
所以,如果我的 JSON 是:
[Type] => Notification
[MessageId] => gibberishhere
[TopicArn] => arn:aws:somethingsomethingsomething
[Message] => {
"notificationType":"Bounce",
"bounce":{
"feedbackId":"blahblahblahblahblah",
"bounceType":"Permanent",
"bounceSubType":"General",
"bouncedRecipients":[{
"emailAddress":"bounce@simulator.amazonses.com",
"action":"failed",
"status":"5.1.1",
"diagnosticCode":"smtp; 550 5.1.1 user unknown"}],
"timestamp":"2020-11-02T16:37:13.000Z",
"remoteMtaIp":"ip.address.here",
"reportingMTA":"dsn; somethingsomething"},
"mail":{
"timestamp":"2020-11-02T16:37:13.029Z",
"source":"test@emailaddress.com",
"sourceArn":"arn:somethingsomethingdotcom",
"sourceIp":"ip.address.here",
"sendingAccountId":"somethingsomething",
"messageId":"numbersnumbersnumbers1234567890",
"destination":["bounce@simulator.amazonses.com"]
}
}
[Timestamp] => 2020-11-02T16:37:13.677Z
[SignatureVersion] => 1
[Signature] => blahblahblah
[SigningCertURL] => blahblahblah
[UnsubscribeURL] => blahblahblah
我用这个来解码它:
$message = json_decode($message, true);
echo $message['Message'];
我得到这个输出:
{
"notificationType":"Bounce",
"bounce":{
"feedbackId":"blahblahblahblahblah",
"bounceType":"Permanent",
"bounceSubType":"General",
"bouncedRecipients":[{
"emailAddress":"bounce@simulator.amazonses.com", <---- I NEED THIS FIELD
"action":"failed",
"status":"5.1.1",
"diagnosticCode":"smtp; 550 5.1.1 user unknown"}],
"timestamp":"2020-11-02T16:37:13.000Z",
"remoteMtaIp":"ip.address.here",
"reportingMTA":"dsn; e226-55.smtp-out.us-east-2.amazonses.com"},
"mail":{
"timestamp":"2020-11-02T16:37:13.029Z",
"source":"test@emailaddress.com",
"sourceArn":"arn:somethingsomethingdotcom",
"sourceIp":"ip.address.here",
"sendingAccountId":"somethingsomething",
"messageId":"numbersnumbers1234567890",
"destination":["bounce@simulator.amazonses.com"]
}
}
我只需要 emailAddress
字段。我需要走多远才能抓住它?我试过了
$message['Message']['bounce']
、$message['Message']['emailAddress']
和其他几个人,但他们都是 return Illegal string offset
.
使用关联数组相对容易,如评论中所建议:
$arr = json_decode($message['Message'], true);
现在您需要做的就是引用正确的元素,遵循路径:
echo $arr['bounce']['bouncedRecipients'][0]['emailAddress'];
给出:
bounce@simulator.amazonses.com
编辑:如何引用元素 emailAddress
- 解释 [0] 索引
为了更好地了解$arr
的数组结构,可以使用:
echo '<pre>';
print_r($arr);
echo '</pre>';
输出:
Array
(
[notificationType] => Bounce
[bounce] => Array
(
[feedbackId] => blahblahblahblahblah
[bounceType] => Permanent
[bounceSubType] => General
[bouncedRecipients] => Array
(
[0] => Array
(
[emailAddress] => bounce@simulator.amazonses.com
[action] => failed
[status] => 5.1.1
[diagnosticCode] => smtp; 550 5.1.1 user unknown
)
)
[timestamp] => 2020-11-02T16:37:13.000Z
[remoteMtaIp] => ip.address.here
[reportingMTA] => dsn; e226-55.smtp-out.us-east-2.amazonses.com
)
[mail] => Array
(
[timestamp] => 2020-11-02T16:37:13.029Z
[source] => test@emailaddress.com
[sourceArn] => arn:somethingsomethingdotcom
[sourceIp] => ip.address.here
[sendingAccountId] => somethingsomething
[messageId] => numbersnumbers1234567890
[destination] => Array
(
[0] => bounce@simulator.amazonses.com
)
)
)
现在您需要做的就是按照 'path' 找到您要引用的元素。 IE。对于 emailAddress
让我们反过来 'route':
emailAddress
存在于键为 [0]
的数组中(你有它,键 [0]
)
键 [0]
存在于键 [bouncedRecipients]
的数组中
键 [bouncedRecipients]
存在于键 [bounce]
的数组中
键 [bounce]
位于数组 $arr
.
至此,元素的路径:
['bounce']['bouncedRecipients'][0]['emailAddress']