php - 从复杂的嵌套 JSON 对象获取数据(使用 PHP)

标签 php json parsing nested decode

我试图从这个复杂的嵌套 JSON 数据中获取时间“15:00:00”的最低最高温度。谁能帮助我如何提取数据?

JSON 数据如下所示:

{
    "city": {
        "id": 1609350,
        "name": "Bangkok",
        "coord": {
            "lon": 100.51667,
            "lat": 13.75
        },
        "country": "TH",
        "population": 0,
        "sys": {
            "population": 0
        }
    },
    "cod": "200",
    "message": 0.0169,
    "cnt": 38,
    "list": [{
        "dt": 1466402400,
        "main": {
            "temp": 311.79,
            "temp_min": 308.315,
            "temp_max": 311.79,
            "pressure": 1018.37,
            "sea_level": 1018.77,
            "grnd_level": 1018.37,
            "humidity": 61,
            "temp_kf": 3.47
        },
        "weather": [{
            "id": 801,
            "main": "Clouds",
            "description": "few clouds",
            "icon": "02d"
        }],
        "clouds": {
            "all": 24
        },
        "wind": {
            "speed": 2.14,
            "deg": 269.001
        },
        "sys": {
            "pod": "d"
        },
        "dt_txt": "2016-06-20 06:00:00"
    }, {
        "dt": 1466413200,
        "main": {
            "temp": 309.22,
            "temp_min": 306.611,
            "temp_max": 309.22,
            "pressure": 1016.83,
            "sea_level": 1017.28,
            "grnd_level": 1016.83,
            "humidity": 61,
            "temp_kf": 2.6
        },
        "weather": [{
            "id": 500,
            "main": "Rain",
            "description": "light rain",
            "icon": "10d"
        }],
        "clouds": {
            "all": 36
        },
        "wind": {
            "speed": 1.07,
            "deg": 132.503
        },
        "rain": {
            "3h": 1.88
        },
        "sys": {
            "pod": "d"
        },
        "dt_txt": "2016-06-20 09:00:00"
    }, {
        "dt": 1466424000,
        "main": {
            "temp": 307.17,
            "temp_min": 305.429,
            "temp_max": 307.17,
            "pressure": 1017.62,
            "sea_level": 1018.09,
            "grnd_level": 1017.62,
            "humidity": 59,
            "temp_kf": 1.74
        },
        "weather": [{
            "id": 804,
            "main": "Clouds",
            "description": "overcast clouds",
            "icon": "04n"
        }],
        "clouds": {
            "all": 92
        },
        "wind": {
            "speed": 1.33,
            "deg": 153.501
        },
        "rain": {},
        "sys": {
            "pod": "n"
        },
        "dt_txt": "2016-06-20 12:00:00"
    }, {
        "dt": 1466434800,
        "main": {
            "temp": 303.96,
            "temp_min": 303.092,
            "temp_max": 303.96,
            "pressure": 1019.69,
            "sea_level": 1020.02,
            "grnd_level": 1019.69,
            "humidity": 66,
            "temp_kf": 0.87
        },
        "weather": [{
            "id": 803,
            "main": "Clouds",
            "description": "broken clouds",
            "icon": "04n"
        }],
        "clouds": {
            "all": 76
        },
        "wind": {
            "speed": 4.26,
            "deg": 180.504
        },
        "rain": {},
        "sys": {
            "pod": "n"
        },
        "dt_txt": "2016-06-20 15:00:00"
    }, {
        "dt": 1466445600,
        "main": {
            "temp": 301.672,
            "temp_min": 301.672,
            "temp_max": 301.672,
            "pressure": 1019.5,
            "sea_level": 1019.9,
            "grnd_level": 1019.5,
            "humidity": 74,
            "temp_kf": 0
        },
        "weather": [{
            "id": 802,
            "main": "Clouds",
            "description": "scattered clouds",
            "icon": "03n"
        }],
        "clouds": {
            "all": 48
        },
        "wind": {
            "speed": 3.06,
            "deg": 163.501
        },
        "rain": {},
        "sys": {
            "pod": "n"
        },
        "dt_txt": "2016-06-20 18:00:00"
    }, {
        "dt": 1466456400,
        "main": {
            "temp": 300.708,
            "temp_min": 300.708,
            "temp_max": 300.708,
            "pressure": 1018.73,
            "sea_level": 1019.13,
            "grnd_level": 1018.73,
            "humidity": 82,
            "temp_kf": 0
        },
        "weather": [{
            "id": 803,
            "main": "Clouds",
            "description": "broken clouds",
            "icon": "04n"
        }],
        "clouds": {
            "all": 68
        },
        "wind": {
            "speed": 1.66,
            "deg": 141.5
        },
        "rain": {},
        "sys": {
            "pod": "n"
        },
        "dt_txt": "2016-06-20 21:00:00"
    }, {
        "dt": 1466467200,
        "main": {
            "temp": 301.217,
            "temp_min": 301.217,
            "temp_max": 301.217,
            "pressure": 1020.18,
            "sea_level": 1020.61,
            "grnd_level": 1020.18,
            "humidity": 79,
            "temp_kf": 0
        },
        "weather": [{
            "id": 803,
            "main": "Clouds",
            "description": "broken clouds",
            "icon": "04d"
        }],
        "clouds": {
            "all": 68
        },
        "wind": {
            "speed": 1.61,
            "deg": 91.0013
        },
        "rain": {},
        "sys": {
            "pod": "d"
        },
        "dt_txt": "2016-06-21 00:00:00"
    }, {
        "dt": 1466478000,
        "main": {
            "temp": 306.368,
            "temp_min": 306.368,
            "temp_max": 306.368,
            "pressure": 1020.81,
            "sea_level": 1021.3,
            "grnd_level": 1020.81,
            "humidity": 62,
            "temp_kf": 0
        },
        "weather": [{
            "id": 802,
            "main": "Clouds",
            "description": "scattered clouds",
            "icon": "03d"
        }],
        "clouds": {
            "all": 36
        },
        "wind": {
            "speed": 2.86,
            "deg": 298.001
        },
        "rain": {},
        "sys": {
            "pod": "d"
        },
        "dt_txt": "2016-06-21 03:00:00"
    }, {
        "dt": 1466488800,
        "main": {
            "temp": 308.988,
            "temp_min": 308.988,
            "temp_max": 308.988,
            "pressure": 1019.11,
            "sea_level": 1019.63,
            "grnd_level": 1019.11,
            "humidity": 49,
            "temp_kf": 0
        },
        "weather": [{
            "id": 802,
            "main": "Clouds",
            "description": "scattered clouds",
            "icon": "03d"
        }],
        "clouds": {
            "all": 44
        },
        "wind": {
            "speed": 4.12,
            "deg": 266.505
        },
        "rain": {},
        "sys": {
            "pod": "d"
        },
        "dt_txt": "2016-06-21 06:00:00"
    }, {
        "dt": 1466499600,
        "main": {
            "temp": 305.343,
            "temp_min": 305.343,
            "temp_max": 305.343,
            "pressure": 1017.43,
            "sea_level": 1017.76,
            "grnd_level": 1017.43,
            "humidity": 67,
            "temp_kf": 0
        },
        "weather": [{
            "id": 500,
            "main": "Rain",
            "description": "light rain",
            "icon": "10d"
        }],
        "clouds": {
            "all": 36
        },
        "wind": {
            "speed": 3.87,
            "deg": 212.501
        },
        "rain": {
            "3h": 1.415
        },
        "sys": {
            "pod": "d"
        },
        "dt_txt": "2016-06-21 09:00:00"
    }, {
        "dt": 1466510400,
        "main": {
            "temp": 304.423,
            "temp_min": 304.423,
            "temp_max": 304.423,
            "pressure": 1018.4,
            "sea_level": 1018.97,
            "grnd_level": 1018.4,
            "humidity": 63,
            "temp_kf": 0
        },
        "weather": [{
            "id": 500,
            "main": "Rain",
            "description": "light rain",
            "icon": "10n"
        }],
        "clouds": {
            "all": 92
        },
        "wind": {
            "speed": 2.05,
            "deg": 196.508
        },
        "rain": {
            "3h": 1.075
        },
        "sys": {
            "pod": "n"
        },
        "dt_txt": "2016-06-21 12:00:00"
    }, {
        "dt": 1466521200,
        "main": {
            "temp": 303.07,
            "temp_min": 303.07,
            "temp_max": 303.07,
            "pressure": 1020,
            "sea_level": 1020.58,
            "grnd_level": 1020,
            "humidity": 67,
            "temp_kf": 0
        },
        "weather": [{
            "id": 803,
            "main": "Clouds",
            "description": "broken clouds",
            "icon": "04n"
        }],
        "clouds": {
            "all": 68
        },
        "wind": {
            "speed": 2.56,
            "deg": 178.501
        },
        "rain": {},
        "sys": {
            "pod": "n"
        },
        "dt_txt": "2016-06-21 15:00:00"
    }, {
        "dt": 1466532000,
        "main": {
            "temp": 300.893,
            "temp_min": 300.893,
            "temp_max": 300.893,
            "pressure": 1020.32,
            "sea_level": 1020.72,
            "grnd_level": 1020.32,
            "humidity": 80,
            "temp_kf": 0
        },
        "weather": [{
            "id": 500,
            "main": "Rain",
            "description": "light rain",
            "icon": "10n"
        }],
        "clouds": {
            "all": 56
        },
        "wind": {
            "speed": 1.62,
            "deg": 242.5
        },
        "rain": {
            "3h": 1.735
        },
        "sys": {
            "pod": "n"
        },
        "dt_txt": "2016-06-21 18:00:00"
    }, {
        "dt": 1466542800,
        "main": {
            "temp": 300.177,
            "temp_min": 300.177,
            "temp_max": 300.177,
            "pressure": 1019.56,
            "sea_level": 1020.01,
            "grnd_level": 1019.56,
            "humidity": 79,
            "temp_kf": 0
        },
        "weather": [{
            "id": 803,
            "main": "Clouds",
            "description": "broken clouds",
            "icon": "04n"
        }],
        "clouds": {
            "all": 56
        },
        "wind": {
            "speed": 1.11,
            "deg": 237.002
        },
        "rain": {},
        "sys": {
            "pod": "n"
        },
        "dt_txt": "2016-06-21 21:00:00"
    }, {
        "dt": 1466553600,
        "main": {
            "temp": 301.577,
            "temp_min": 301.577,
            "temp_max": 301.577,
            "pressure": 1020.42,
            "sea_level": 1020.8,
            "grnd_level": 1020.42,
            "humidity": 74,
            "temp_kf": 0
        },
        "weather": [{
            "id": 802,
            "main": "Clouds",
            "description": "scattered clouds",
            "icon": "03d"
        }],
        "clouds": {
            "all": 48
        },
        "wind": {
            "speed": 2.11,
            "deg": 288.01
        },
        "rain": {},
        "sys": {
            "pod": "d"
        },
        "dt_txt": "2016-06-22 00:00:00"
    }]
}

最佳答案

  <?php
$data= '{
  "city": {
    "id": 1609350,
    "name": "Bangkok",
    "coord": {
      "lon": 100.51667,
      "lat": 13.75
    },
    "country": "TH",
    "population": 0,
    "sys": {
      "population": 0
    }
  },
  "cod": "200",
  "message": 0.0169,
  "cnt": 38,
  "list": [
    {
      "dt": 1466402400,
      "main": {
        "temp": 311.79,
        "temp_min": 308.315,
        "temp_max": 311.79,
        "pressure": 1018.37,
        "sea_level": 1018.77,
        "grnd_level": 1018.37,
        "humidity": 61,
        "temp_kf": 3.47
      },
      "weather": [
        {
          "id": 801,
          "main": "Clouds",
          "description": "few clouds",
          "icon": "02d"
        }
      ],
      "clouds": {
        "all": 24
      },
      "wind": {
        "speed": 2.14,
        "deg": 269.001
      },
      "sys": {
        "pod": "d"
      },
      "dt_txt": "2016-06-20 06:00:00"
    },
    {
      "dt": 1466413200,
      "main": {
        "temp": 309.22,
        "temp_min": 306.611,
        "temp_max": 309.22,
        "pressure": 1016.83,
        "sea_level": 1017.28,
        "grnd_level": 1016.83,
        "humidity": 61,
        "temp_kf": 2.6
      },
      "weather": [
        {
          "id": 500,
          "main": "Rain",
          "description": "light rain",
          "icon": "10d"
        }
      ],
      "clouds": {
        "all": 36
      },
      "wind": {
        "speed": 1.07,
        "deg": 132.503
      },
      "rain": {
        "3h": 1.88
      },
      "sys": {
        "pod": "d"
      },
      "dt_txt": "2016-06-20 09:00:00"
    },
    {
      "dt": 1466424000,
      "main": {
        "temp": 307.17,
        "temp_min": 305.429,
        "temp_max": 307.17,
        "pressure": 1017.62,
        "sea_level": 1018.09,
        "grnd_level": 1017.62,
        "humidity": 59,
        "temp_kf": 1.74
      },
      "weather": [
        {
          "id": 804,
          "main": "Clouds",
          "description": "overcast clouds",
          "icon": "04n"
        }
      ],
      "clouds": {
        "all": 92
      },
      "wind": {
        "speed": 1.33,
        "deg": 153.501
      },
      "rain": {},
      "sys": {
        "pod": "n"
      },
      "dt_txt": "2016-06-20 12:00:00"
    },
    {
      "dt": 1466434800,
      "main": {
        "temp": 303.96,
        "temp_min": 303.092,
        "temp_max": 303.96,
        "pressure": 1019.69,
        "sea_level": 1020.02,
        "grnd_level": 1019.69,
        "humidity": 66,
        "temp_kf": 0.87
      },
      "weather": [
        {
          "id": 803,
          "main": "Clouds",
          "description": "broken clouds",
          "icon": "04n"
        }
      ],
      "clouds": {
        "all": 76
      },
      "wind": {
        "speed": 4.26,
        "deg": 180.504
      },
      "rain": {},
      "sys": {
        "pod": "n"
      },
      "dt_txt": "2016-06-20 15:00:00"
    },
    {
      "dt": 1466445600,
      "main": {
        "temp": 301.672,
        "temp_min": 301.672,
        "temp_max": 301.672,
        "pressure": 1019.5,
        "sea_level": 1019.9,
        "grnd_level": 1019.5,
        "humidity": 74,
        "temp_kf": 0
      },
      "weather": [
        {
          "id": 802,
          "main": "Clouds",
          "description": "scattered clouds",
          "icon": "03n"
        }
      ],
      "clouds": {
        "all": 48
      },
      "wind": {
        "speed": 3.06,
        "deg": 163.501
      },
      "rain": {},
      "sys": {
        "pod": "n"
      },
      "dt_txt": "2016-06-20 18:00:00"
    },
    {
      "dt": 1466456400,
      "main": {
        "temp": 300.708,
        "temp_min": 300.708,
        "temp_max": 300.708,
        "pressure": 1018.73,
        "sea_level": 1019.13,
        "grnd_level": 1018.73,
        "humidity": 82,
        "temp_kf": 0
      },
      "weather": [
        {
          "id": 803,
          "main": "Clouds",
          "description": "broken clouds",
          "icon": "04n"
        }
      ],
      "clouds": {
        "all": 68
      },
      "wind": {
        "speed": 1.66,
        "deg": 141.5
      },
      "rain": {},
      "sys": {
        "pod": "n"
      },
      "dt_txt": "2016-06-20 21:00:00"
    },
    {
      "dt": 1466467200,
      "main": {
        "temp": 301.217,
        "temp_min": 301.217,
        "temp_max": 301.217,
        "pressure": 1020.18,
        "sea_level": 1020.61,
        "grnd_level": 1020.18,
        "humidity": 79,
        "temp_kf": 0
      },
      "weather": [
        {
          "id": 803,
          "main": "Clouds",
          "description": "broken clouds",
          "icon": "04d"
        }
      ],
      "clouds": {
        "all": 68
      },
      "wind": {
        "speed": 1.61,
        "deg": 91.0013
      },
      "rain": {},
      "sys": {
        "pod": "d"
      },
      "dt_txt": "2016-06-21 00:00:00"
    },
    {
      "dt": 1466478000,
      "main": {
        "temp": 306.368,
        "temp_min": 306.368,
        "temp_max": 306.368,
        "pressure": 1020.81,
        "sea_level": 1021.3,
        "grnd_level": 1020.81,
        "humidity": 62,
        "temp_kf": 0
      },
      "weather": [
        {
          "id": 802,
          "main": "Clouds",
          "description": "scattered clouds",
          "icon": "03d"
        }
      ],
      "clouds": {
        "all": 36
      },
      "wind": {
        "speed": 2.86,
        "deg": 298.001
      },
      "rain": {},
      "sys": {
        "pod": "d"
      },
      "dt_txt": "2016-06-21 03:00:00"
    },
    {
      "dt": 1466488800,
      "main": {
        "temp": 308.988,
        "temp_min": 308.988,
        "temp_max": 308.988,
        "pressure": 1019.11,
        "sea_level": 1019.63,
        "grnd_level": 1019.11,
        "humidity": 49,
        "temp_kf": 0
      },
      "weather": [
        {
          "id": 802,
          "main": "Clouds",
          "description": "scattered clouds",
          "icon": "03d"
        }
      ],
      "clouds": {
        "all": 44
      },
      "wind": {
        "speed": 4.12,
        "deg": 266.505
      },
      "rain": {},
      "sys": {
        "pod": "d"
      },
      "dt_txt": "2016-06-21 06:00:00"
    },
    {
      "dt": 1466499600,
      "main": {
        "temp": 305.343,
        "temp_min": 305.343,
        "temp_max": 305.343,
        "pressure": 1017.43,
        "sea_level": 1017.76,
        "grnd_level": 1017.43,
        "humidity": 67,
        "temp_kf": 0
      },
      "weather": [
        {
          "id": 500,
          "main": "Rain",
          "description": "light rain",
          "icon": "10d"
        }
      ],
      "clouds": {
        "all": 36
      },
      "wind": {
        "speed": 3.87,
        "deg": 212.501
      },
      "rain": {
        "3h": 1.415
      },
      "sys": {
        "pod": "d"
      },
      "dt_txt": "2016-06-21 09:00:00"
    },
    {
      "dt": 1466510400,
      "main": {
        "temp": 304.423,
        "temp_min": 304.423,
        "temp_max": 304.423,
        "pressure": 1018.4,
        "sea_level": 1018.97,
        "grnd_level": 1018.4,
        "humidity": 63,
        "temp_kf": 0
      },
      "weather": [
        {
          "id": 500,
          "main": "Rain",
          "description": "light rain",
          "icon": "10n"
        }
      ],
      "clouds": {
        "all": 92
      },
      "wind": {
        "speed": 2.05,
        "deg": 196.508
      },
      "rain": {
        "3h": 1.075
      },
      "sys": {
        "pod": "n"
      },
      "dt_txt": "2016-06-21 12:00:00"
    },
    {
      "dt": 1466521200,
      "main": {
        "temp": 303.07,
        "temp_min": 303.07,
        "temp_max": 303.07,
        "pressure": 1020,
        "sea_level": 1020.58,
        "grnd_level": 1020,
        "humidity": 67,
        "temp_kf": 0
      },
      "weather": [
        {
          "id": 803,
          "main": "Clouds",
          "description": "broken clouds",
          "icon": "04n"
        }
      ],
      "clouds": {
        "all": 68
      },
      "wind": {
        "speed": 2.56,
        "deg": 178.501
      },
      "rain": {},
      "sys": {
        "pod": "n"
      },
      "dt_txt": "2016-06-21 15:00:00"
    },
    {
      "dt": 1466532000,
      "main": {
        "temp": 300.893,
        "temp_min": 300.893,
        "temp_max": 300.893,
        "pressure": 1020.32,
        "sea_level": 1020.72,
        "grnd_level": 1020.32,
        "humidity": 80,
        "temp_kf": 0
      },
      "weather": [
        {
          "id": 500,
          "main": "Rain",
          "description": "light rain",
          "icon": "10n"
        }
      ],
      "clouds": {
        "all": 56
      },
      "wind": {
        "speed": 1.62,
        "deg": 242.5
      },
      "rain": {
        "3h": 1.735
      },
      "sys": {
        "pod": "n"
      },
      "dt_txt": "2016-06-21 18:00:00"
    },
    {
      "dt": 1466542800,
      "main": {
        "temp": 300.177,
        "temp_min": 300.177,
        "temp_max": 300.177,
        "pressure": 1019.56,
        "sea_level": 1020.01,
        "grnd_level": 1019.56,
        "humidity": 79,
        "temp_kf": 0
      },
      "weather": [
        {
          "id": 803,
          "main": "Clouds",
          "description": "broken clouds",
          "icon": "04n"
        }
      ],
      "clouds": {
        "all": 56
      },
      "wind": {
        "speed": 1.11,
        "deg": 237.002
      },
      "rain": {},
      "sys": {
        "pod": "n"
      },
      "dt_txt": "2016-06-21 21:00:00"
    },
    {
      "dt": 1466553600,
      "main": {
        "temp": 301.577,
        "temp_min": 301.577,
        "temp_max": 301.577,
        "pressure": 1020.42,
        "sea_level": 1020.8,
        "grnd_level": 1020.42,
        "humidity": 74,
        "temp_kf": 0
      },
      "weather": [
        {
          "id": 802,
          "main": "Clouds",
          "description": "scattered clouds",
          "icon": "03d"
        }
      ],
      "clouds": {
        "all": 48
      },
      "wind": {
        "speed": 2.11,
        "deg": 288.01
      },
      "rain": {},
      "sys": {
        "pod": "d"
      },
      "dt_txt": "2016-06-22 00:00:00"
    }
  ]
}';

//decoding string into json object
$json=json_decode($data,TRUE);
$list=$json['list'];
for($i=0;$i<sizeof($list);$i++)
{
    $dt_time=$list[$i]['dt_txt'];
    $date = date('Y-m-d',strtotime($dt_time));
    $time = date('H:i:s',strtotime($dt_time));
    if(!strcmp($time, "15:00:00"))
    {
        /*

         now time is 15:00:00
         this is how to get temp_min and max at a particular time 
         you need to traverse json object 

         */

        $temp_min=$list[$i]['main']['temp_min'];
        $temp_max=$list[$i]['main']['temp_max'];
        echo '<br/><br/><h3>'.$date.' Status </h2><br/>';
        echo 'Temp_min: '.$temp_min;
        echo '<br/>Temp_max: '.$temp_max;
    }
}
?>

关于php - 从复杂的嵌套 JSON 对象获取数据(使用 PHP),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37914937/

相关文章:

php - 如何在 Propel 1.6 中按日期过滤日期时间列?

json - 将多个 jq 调用合并为一个

java - 在android中反序列化JSON字符串

c# - 使用 (Try)Parse 将空字符串加倍

python - 如何在正则表达式中使用 bool OR

ios - CollectionView 中加载的图像上出现的名称不正确

php - 转换/下载视频 php 或 javascript

php - CURLOPT_TIMEOUT 的最大值

php - 用 PHP 检查 URL 是否有特定的字符串

node.js - 将标准输出转换为 JSON