我正在通过HTTP POST回呼到PHP页面接收一些JSON,但是在解析JSON时遇到问题。这是发送的JSON数据的示例:
[
{
"type" : "message-received",
"time" : "2016-09-14T18:20:16Z",
"description" : "Incoming message received",
"to" : "+12345678902",
"message" : {
"id" : "14762070468292kw2fuqty55yp2b2",
"time" : "2016-09-14T18:20:16Z",
"to" : ["+12345678902"],
"from" : "+12345678901",
"text" : "Hey, check this out!",
"applicationId" : "93de2206-9669-4e07-948d-329f4b722ee2",
"media" : [
"https://messaging.bandwidth.com/api/v2/users/{accountId}/media/14762070468292kw2fuqty55yp2b2/0/bw.png"
],
"owner" : "+12345678902",
"direction" : "in",
"segmentCount" : 1
}
}
]
然后,我将按照以下方式进行处理:
$eventJSON = file_get_contents('php://input');
$event= json_decode( $eventJSON );
$eventType = $event->type;
但是我的$eventType
变量到目前为止还没有得到什么-我认为问题可能在于JSON是一个数组,但是我不确定如何处理?
答案 0 :(得分:4)
要解析json,请尝试
$eventType = $event[0]->type;
请参阅:-How do I extract data from JSON with PHP?了解对象属性和数组元素之间的区别