简体   繁体   English

解析多级JSON对象/数组

[英]Parsing multi-level JSON Objects/arrays

I have the following json object to parse in php. 我有以下json对象要在php中解析。

I cannot seem to loop through the json to get all these attributes in one go to look like the example below. 我似乎无法遍历json以一次性获得所有这些属性,就像下面的示例一样。

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

the output should look the the below: 输出应如下所示:

20170308
Rain
Mobay

20170307
Clear
Kingston

20170309
Clear
Kingston
......

JSON: 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 code: 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;
    }

How can I do this? 我怎样才能做到这一点?

If you'd use json_decode($json,true) you'd see that: 如果使用json_decode($ json,true),则会看到:

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, 

etc. 等等

In other words: list is an array of arrays try: 换句话说:list是一个数组数组,尝试:

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

$list is an object, not an array, so foreach ($list as $b) makes no sense. $list是一个对象,而不是数组,因此foreach ($list as $b)毫无意义。 It just has a single dt_txt property, so print it once, not inside a nested loop. 它只有一个dt_txt属性,因此只打印一次,而不要在嵌套循环中打印。

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

I've dumped your decoded JSON and this is the resulting PHP object: 我已经转储了解码后的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
        )
)

So, the attributes you want can be found here: 因此,您可以在此处找到所需的属性:

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

The same goes for $mobayJson object. $mobayJson对象也是如此。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM