如何访问返回的json值

时间:2019-02-18 09:30:26

标签: php json

如何在php中访问此变量?

result-> message-> from-> id

我用json_decode()解码了返回的json 我想通过上述方法获得价值

{
    "ok": true,
    "result": [
        {
            "update_id": 176446579,
            "message": {
                "message_id": 7,
                "from": {
                    "id": 468822221,
                    "is_bot": false,
                    "first_name": "M.GH",
                    "last_name": "2003",
                    "username": "moji_2003",
                    "language_code": "fa"
                },
                "chat": {
                    "id": 468822221,
                    "first_name": "M.GH",
                    "last_name": "2003",
                    "username": "moji_2003",
                    "type": "private"
                },
                "date": 1550384619,
                "text": "salam"
            }
        }
    ]
}

2 个答案:

答案 0 :(得分:1)

$result是一个数组,因此您需要先访问该数组中的第一个元素:

$json = json_decode('JSON_STRING');

$id = $json->result[0]->message->form->id;

答案 1 :(得分:1)

是的,您可以通过以下方式访问它:

 $data = json_decode($yourJsonString);
 echo $data->result[0]->message->from->id;