解码php://输入json

时间:2018-01-24 17:12:56

标签: php json input

我的文字来自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和其他人工作。

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

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

2 个答案:

答案 0 :(得分:4)

你有一个数组,所以,你需要得到第一个元素:

$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

答案 1 :(得分:-2)

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

<?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;