如何访问 json 中的特定元素

how can I access to a specific element in json

我有一个来自电报的 JSON,在 php 中如何发现如果它是文本做某事,但如果它是照片、音频或...做其他事情。这部分位于日期之后,我该如何访问它?

我尝试像 ['message'][1] 这样通过索引访问它,但我得到了错误。

{
  "ok": true,
  "result": [
    {
      "update_id": 30213598,
      "message": {
        "message_id": 41,
        "from": {
          "id": 102768333,
          "first_name": "QWr_1",
          "username": "john"
        },
        "chat": {
          "id": 108768733,
          "first_name": "QWr_1",
          "username": "john"
        },
        "date": 1439233778,
        "text": "this is a test"
      }
    },
        {
      "update_id": 30213599,
      "message": {
        "message_id": 46,
        "from": {
          "id": 108768733,
          "first_name": "QWr_1",
          "username": "john"
        },
        "chat": {
          "id": 108768733,
          "first_name": "QWr_1",
          "username": "john"
        },
        "date": 1439234126,
        "photo": [
          {
            "file_id": "AgADBAAD4acxG92tewa8uRLrpBBo-h1icjAABCohC6S9uPdhViIAAgI",
            "file_size": 1923,
            "width": 90,
            "height": 90
          },
          {
            "file_id": "AgADBAAD4acxG92tewa8uRLrpBBo-h1icjAABOT4Q5CJoFOVVSIAAgI",
            "file_size": 24105,
            "width": 320,
            "height": 320
          },
          {
            "file_id": "AgADBAAD4acxG92tewa8uRLrpBBo-h1icjAABK2WgDnJ74jlVCIAAgI",
            "file_size": 57650,
            "width": 640,
            "height": 640
          }
        ]
      }
    },

您需要使用json_decode功能。

我使用 foreach 循环然后 echo $item['message']['message_id'] 在以下示例中显示 message_id

尝试示例

$json = <<<JSON
{
  "ok": true,
  "result": [
    {
      "update_id": 30213598,
      "message": {
        "message_id": 41,
        "from": {
          "id": 102768333,
          "first_name": "QWr_1",
          "username": "john"
        },
        "chat": {
          "id": 108768733,
          "first_name": "QWr_1",
          "username": "john"
        },
        "date": 1439233778,
        "text": "this is a test"
      }
    },
        {
      "update_id": 30213599,
      "message": {
        "message_id": 46,
        "from": {
          "id": 108768733,
          "first_name": "QWr_1",
          "username": "john"
        },
        "chat": {
          "id": 108768733,
          "first_name": "QWr_1",
          "username": "john"
        },
        "date": 1439234126,
        "photo": [
          {
            "file_id": "AgADBAAD4acxG92tewa8uRLrpBBo-h1icjAABCohC6S9uPdhViIAAgI",
            "file_size": 1923,
            "width": 90,
            "height": 90
          },
          {
            "file_id": "AgADBAAD4acxG92tewa8uRLrpBBo-h1icjAABOT4Q5CJoFOVVSIAAgI",
            "file_size": 24105,
            "width": 320,
            "height": 320
          },
          {
            "file_id": "AgADBAAD4acxG92tewa8uRLrpBBo-h1icjAABK2WgDnJ74jlVCIAAgI",
            "file_size": 57650,
            "width": 640,
            "height": 640
          }
        ]
      }
    }
]
}
JSON;

$json_data = json_decode($json, true);

foreach($json_data['result'] as $item)
{
    if(isset($item['message']['text']))
    {
        echo $item['message']['text']."<br />";
    }

    if(isset($item['message']['photo']))
    {
        foreach($item['message']['photo'] as $photo)
        {
            echo "file_id: ".$photo['file_id']."<br />";
            echo "file_size: ".$photo['file_size']."<br />";
        }
    }
}