猿问

PHP - 解析 JSON 有效负载问题

我通过对 PHP 页面的 HTTP POST 回调接收到一些 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 是一个数组,但我不确定如何处理这个?


白衣非少年
浏览 167回答 1
1回答

慕容708150

解析 json 尝试$eventType = $event[0]->type;参考 :-如何使用 PHP 从 JSON 中提取数据?了解对象属性和数组元素的区别
随时随地看视频慕课网APP
我要回答