简体   繁体   English

解码php:// input json

[英]Decode php://input json

I have this text from file_get_contents('php://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}]

I need to get single element like email or event, but haven't been able to get json_decode and others to work. 我需要获取单个元素,如电子邮件或事件,但无法获取json_decode和其他json_decode才能正常工作。

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

How can I reference a single element in the json data? 如何引用json数据中的单个元素?

You have an array, so, you need to get the first element: 您有一个数组,因此,需要获取第一个元素:

$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

Here you go, here's a fully working answer: 到这里,这是一个完全有效的答案:

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