繁体   English   中英

解码php:// input json

[英]Decode php://input json

我有来自file_get_contents('php://input')这段文字:

[{"email":"text@examlple.com","event":"processed","sg_event_id":"JJTr9qA","sg_message_id":"AjvX5L7IQeGOfxmJV-OCnw","smtp-id":"<AfxmJV-OCnw@ismon1.sendgrid.net>","timestamp":16813363}]

我需要获取单个元素,如电子邮件或事件,但无法获取json_decode和其他json_decode才能正常工作。

$obj = json_decode(file_get_contents('php://input'));

如何引用json数据中的单个元素?

您有一个数组,因此,需要获取第一个元素:

$json = '[{"email":"text@examlple.com","event":"processed","sg_event_id":"JJTr9qA","sg_message_id":"AjvX5L7IQeGOfxmJV-OCnw","smtp-id":"<AfxmJV-OCnw@ismon1.sendgrid.net>","timestamp":16813363}]';

$obj = json_decode($json);
echo $obj[0]->email; //output text@examlple.com

到这里,这是一个完全有效的答案:

<?php

$str = '[{"email":"text@examlple.com","event":"processed","sg_event_id":"JJTr9qA","sg_message_id":"AjvX5L7IQeGOfxmJV-OCnw","smtp-id":"<AfxmJV-OCnw@ismon1.sendgrid.net>","timestamp":16813363}]';

$obj = json_decode($str);

echo $obj[0]->email;

暂无
暂无

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

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