dongzen5577 2015-08-11 06:27
浏览 129
已采纳

如何访问json中的特定元素

I have a JSON from telegram, In php How can find that if it is a text do something, but if its photo, audio or ... do something else. this part is located after date, how can I access it?

I tried by access it by index like ['message'][1] but I got error.

{
  "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
          }
        ]
      }
    },
  • 写回答

1条回答 默认 最新

  • dragam0217 2015-08-11 06:32
    关注

    You need to use json_decode function.

    I use foreach loop then echo $item['message']['message_id'] to display message_id in following example.

    Try Example

    $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 />";
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 Keil uVision5创建project没反应
  • ¥15 mmseqs内存报错
  • ¥15 vika文档如何与obsidian同步
  • ¥15 华为手机相册里面的照片能够替换成自己想要的照片吗?
  • ¥15 陆空双模式无人机飞控设置
  • ¥15 sentaurus lithography
  • ¥100 求抖音ck号 或者提ck教程
  • ¥15 关于#linux#的问题:子进程1等待子进程A、B退出后退出(语言-c语言)
  • ¥20 web页面如何打开Outlook 365的全球离线通讯簿功能
  • ¥15 io.jsonwebtoken.security.Keys