2017-03-09 3 views
0

PHPで解析する次のjsonオブジェクトがあります。複数レベルのJSONオブジェクト/配列の解析

私はこれらの属性をすべて取得するためにjsonをループすることはできません。下の例のようになります。

list->dt_txtたとえば、 20170308 list->weather->mainなど。雨 city->nameたとえば、モベイ

出力は以下になります。

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

私はこれをどのように行うことができますか?あなたはjson_decode($のJSON、true)を使用したい場合は

+0

'$ list-> dt'は配列ではなく数字ですなぜ' foreach'を使用していますか? – Barmar

+0

エラーは '$ list'でなければなりません。 –

+1

'print_r($ kingstonWeather);'を使用してキングストンの気象データを含むPHPオブジェクトのフレンドリーな出力を見ると、ターゲット属性の配置場所を確認できるはずです。 –

答えて

0

はオブジェクトであり配列ではないため、foreach ($list as $b)は意味がありません。それは単一のdt_txtプロパティを持っているだけなので、ネストされたループ内ではなく、一度出力してください。私はあなたのデコードされたJSONをダンプしてきたし、これは結果としてPHPオブジェクトです

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

ありがとう@Barmarこれは素晴らしい作品 –

0

あなたはそれを参照してくださいね。

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

しかし、彼は2番目の引数を使用していないので、配列の配列ではなくオブジェクトの配列を取得します。だから '$ list-> dt'は正しいです。 – Barmar

+0

あなたは正しいです。私が選んだ言葉は、コードで使われたdataTypeを表していませんでした。それにもかかわらず、私が作ったのはリストのリストがあり、反復がより深く進む必要があるということです。しかし、もう一度あなたは正しいです。 jsonオブジェクトの表現であるstdClassを使用する場合、foreachでは使用できません。 –

0

:だから

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オブジェクトです。

関連する問題