解析多级JSON对象/数组

时间:2017-03-09 00:39:07

标签: php json

我在php中解析了以下json对象。

我似乎无法遍历json以一次性获取所有这些属性,看起来像下面的示例。

list->dt_txt例如。 20170308 list->weather->main例如。雨 city->name例如。 Mobay公司

输出应如下所示:

20170308
Rain
Mobay

20170307
Clear
Kingston

20170309
Clear
Kingston
......

JSON:

{  
   "cod":"200",
   "message":0.2902,
   "cnt":35,
   "list": [  
      {  
         "dt":1488985200,
         "main":{  
            "temp":300.1,
            "temp_min":299.712,
            "temp_max":300.1,
            "pressure":1026.69,
            "sea_level":1033.03,
            "grnd_level":1026.69,
            "humidity":100,
            "temp_kf":0.39
         },
         "weather": [  
            {  
               "id":800,
               "main":"Clear",
               "description":"clear sky",
               "icon":"01d"
            }
         ],
         "clouds":{  
            "all":0
         },
         "wind":{  
            "speed":9.02,
            "deg":68.0006
         },
         "sys":{  
            "pod":"d"
         },
         "dt_txt":"2017-03-08 15:00:00"
      },
      {  
         "dt":1488996000,
         "main":{  
            "temp":300.55,
            "temp_min":300.252,
            "temp_max":300.55,
            "pressure":1025.2,
            "sea_level":1031.44,
            "grnd_level":1025.2,
            "humidity":98,
            "temp_kf":0.29
         },
         "weather": [  
            {  
               "id":800,
               "main":"Clear",
               "description":"clear sky",
               "icon":"01d"
            }
         ],
         "clouds":{  
            "all":0
         },
         "wind":{  
            "speed":9.07,
            "deg":67.5009
         },
         "sys":{  
            "pod":"d"
         },
         "dt_txt":"2017-03-08 18:00:00"
      }],
      "city":{  
      "id":3489460,
      "name":"Montego Bay",
      "coord":{  
         "lat":18.4712,
         "lon":-77.9189
      },
      "country":"JM"
   }
}

PHP代码:

$kingstonJson =      file_get_contents('http://api.openweathermap.org/data/2.5/forecast?q=Montego%20Bay,Jam&mode=json&appid=894ae60546cfa979ee945b2a7809f23d');
$mobayJson = file_get_contents('http://api.openweathermap.org/data/2.5/forecast?q=Kingston,Jam&mode=json&appid=894ae60546cfa979ee945b2a7809f23d');

$kingstonWeather = json_decode($kingstonJson);
$mobayWeather = json_decode($mobayJson);


foreach($kingstonWeather->list as $list){
//echo $list->weather->main;
    foreach ($list as $b){
        //echo $b;// getting an error here
    }
    foreach ($list->weather as $b){
       echo $b->main;
    }

我该怎么做?

3 个答案:

答案 0 :(得分:0)

如果您使用json_decode($ json,true),您会看到:

In [6]: x.assign(**{lst_col:np.concatenate(data[lst_col].values)})[data.columns.tolist()]
Out[6]:
  Product Category  Product Cost                    Products
0            Music            55     Rock On Leather Journal
1         Journals            55     Rock On Leather Journal
2            Paper            55     Rock On Leather Journal
3       Headphones           163  Beats Earbuds In Ear Timer
4            Music           163  Beats Earbuds In Ear Timer
5           Clocks           163  Beats Earbuds In Ear Timer
6          Watches           200      Garmin 25mm Wristwatch
7           Clocks           200      Garmin 25mm Wristwatch

换句话说:list是一个数组数组try:

array ( 
    'cod' => '200', 
    'message' => 0.29020000000000001, 
    'cnt' => 35, 
    'list' => array ( 
          0 => array ( 
               'dt' => 1488985200, 
               'main' => array ( 
                    'temp' => 300.10000000000002, 
                    'temp_min' => 299.71199999999999, 
                    'temp_max' => 300.10000000000002, 

答案 1 :(得分:0)

$list是一个对象,而不是一个数组,因此foreach ($list as $b)毫无意义。它只有一个dt_txt属性,所以打印一次,而不是嵌套循环。

foreach $(kingstonWeather->list as $list) {
    echo $list->dt_txt . "<br>";
    foreach ($list->weather as $weather) {
        echo $weather->main . "<br>";
    }
}

答案 2 :(得分:0)

我已经转储了您已解码的JSON,这是生成的PHP对象:

stdClass Object
(
    [cod] => 200
    [message] => 0.2902
    [cnt] => 35
    [list] => Array
        (
            [0] => stdClass Object
                (
                    [dt] => 1488985200
                    [main] => stdClass Object
                        (
                            [temp] => 300.1
                            [temp_min] => 299.712
                            [temp_max] => 300.1
                            [pressure] => 1026.69
                            [sea_level] => 1033.03
                            [grnd_level] => 1026.69
                            [humidity] => 100
                            [temp_kf] => 0.39
                        )

                    [weather] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [id] => 800
                                    [main] => Clear
                                    [description] => clear sky
                                    [icon] => 01d
                                )

                        )

                    [clouds] => stdClass Object
                        (
                            [all] => 0
                        )

                    [wind] => stdClass Object
                        (
                            [speed] => 9.02
                            [deg] => 68.0006
                        )

                    [sys] => stdClass Object
                        (
                            [pod] => d
                        )

                    [dt_txt] => 2017-03-08 15:00:00
                )

            [1] => stdClass Object
                (
                    [dt] => 1488996000
                    [main] => stdClass Object
                        (
                            [temp] => 300.55
                            [temp_min] => 300.252
                            [temp_max] => 300.55
                            [pressure] => 1025.2
                            [sea_level] => 1031.44
                            [grnd_level] => 1025.2
                            [humidity] => 98
                            [temp_kf] => 0.29
                        )

                    [weather] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [id] => 800
                                    [main] => Clear
                                    [description] => clear sky
                                    [icon] => 01d
                                )

                        )

                    [clouds] => stdClass Object
                        (
                            [all] => 0
                        )

                    [wind] => stdClass Object
                        (
                            [speed] => 9.07
                            [deg] => 67.5009
                        )

                    [sys] => stdClass Object
                        (
                            [pod] => d
                        )

                    [dt_txt] => 2017-03-08 18:00:00
                )

        )

    [city] => stdClass Object
        (
            [id] => 3489460
            [name] => Montego Bay
            [coord] => stdClass Object
                (
                    [lat] => 18.4712
                    [lon] => -77.9189
                )

            [country] => JM
        )
)

因此,您可以在此处找到所需的属性:

foreach($kingstonWeather->list as $list) {
    $dt_txt = $list->dt_txt; // dt_txt attribute
    foreach($list->weather as $weather) {
        $main_weather = $weather->main; // main weather attribute
    }
}
$city = $kingstonWeather->city->name; // city name attribute

$mobayJson对象也是如此。

相关问题