2017-03-09 172 views
0

我有以下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; 
    } 

我怎樣才能做到這一點?

+0

'$ list-> dt'是一個數字,不是數組,你爲什麼使用'foreach'? – Barmar

+0

這是一個錯誤應該是'$ list'。 –

+1

使用'print_r($ kingstonWeather);'查看包含Kingston天氣數據的PHP對象的友好輸出,這樣您就能夠看到目標屬性的放置位置。 –

回答

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>"; 
    } 
} 
+0

感謝@Barmar這個偉大的工程 –

0

如果您想使用json_decode($ JSON,真)你會看到:

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, 

換句話說:名單是一個數組的數組嘗試:

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; 
     } 
    } 
} 
+1

但他沒有使用第二個參數,因此他獲取了一個對象數組,而不是數組數組。所以'$ list-> dt'是正確的。 – Barmar

+0

你說得對。我選擇的詞不代表代碼中使用的數據類型。儘管如此,我所做的一點是有一個列表清單,所以迭代必須更深層次。但是,你又是對的。如果你使用stdClass表示一個json對象,那麼你不能使用foreach。 –

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對象。