php - 如何解析这种类型的 JSON [PHP]
问题描述
这是下面我要解析的 JSON
{"events": [{"eventId": 2, "payload": {"chat": {"chatId": "683473108@chat.agent", "title": "TrojanTest", "type": "group"}, "from": {"firstName": "Khan", "nick": "Mr.Tr0J4n", "userId": "751401693"}, "msgId": "6895591502924218369", "text": "524545", "timestamp": 1605505008}, "type": "newMessage"}], "ok": true}
我想从这个 JSON中解析msgId
和解析。chatId
我怎样才能在 PHP 中做到这一点?
解决方案
将 json 字符串解码为数组:
$jsonString = '{"events": [{"eventId": 2, "payload": {"chat": {"chatId": "683473108@chat.agent", "title": "TrojanTest", "type": "group"}, "from": {"firstName": "Khan", "nick": "Mr.Tr0J4n", "userId": "751401693"}, "msgId": "6895591502924218369", "text": "524545", "timestamp": 1605505008}, "type": "newMessage"}], "ok": true}';
$arr = json_decode($jsonString, true);
从数组中获取:
$chatId = $arr['events'][0]['payload']['chat']['chatId'];
$msgId = $arr['events'][0]['payload']['msgId'];
var_dump($chatId);
var_dump($msgId);
结果将是:
string(20) "683473108@chat.agent"
string(19) "6895591502924218369"
推荐阅读
- python - 为什么它不接受函数中的数据
- asp.net-mvc - Asp.Net Core Web App 从磁盘播放和下载视频
- python - 我想我在更新 heroku 上的机器人时遇到了错误
- java - 线性布局格式一直显示错误,我已经尽我所能但仍然无法正常工作。?
- java - 如何安排任务将每月电子邮件发送到不同的时区?
- arrays - Lua 中的数组节点
- flutter - 向 Linux VM 发送命令行参数,接收输出,全部来自 Flutter 应用程序
- events - Yii2 Activerecord 在重定向之前未保存并显示在“视图”中
- javascript - 如何直接编辑尚未应用于元素的类的 CSS 属性(仅限 JavaScript)?
- ios - 在应用商店添加应用时出现错误