2016-05-06 11 views
0

私はWeatherアプリケーションを作成しようとしています。私は現在の条件を取得する方法を知っていますが、正直なところ私はあまりJSONを知らない。 OpenWeatherMapから7日間の予測を取得する方法はわかりません。私は考えることができるすべてを試しましたが、何も動いていません。私はPHPからそれを取得しようとしています。私は本当にそれがどのように動作するか、正直には分かりません。誰かが助けることができれば、それは高く評価されるでしょう。JSONデータを使用してOpenWeatherMapから7日間の予測を取得するにはどうすればよいですか?

['city'] ['name'];を入力します。例のように。

私のPHPコード:

<?php 
$city="London"; 
$country="UK"; 
$url="http://api.openweathermap.org/data/2.5/forecast/daily?q=".$city.",".$country."&units=metric&cnt=7&lang=en&appid=c0c4a4b4047b97ebc5948ac9c48c0559"; 
$json=file_get_contents($url); 
$data=json_decode($json,true); 
$data['city']['name']; 
?> 

は、ここでJSONデータである:これは本当にばかな質問ですが、任意の助けをいただければ幸い場合

"city":{ 
"id":2643743, 
"name":"London", 
"coord":{ 
"lon":-0.12574, 
"lat":51.50853 
}, 
"country":"GB", 
"population":0 
}, 
"cod":"200", 
"message":0.0132, 
"cnt":7, 
"list":[ 
{ 
"dt":1462532400, 
"temp":{ 
"day":15.49, 
"min":13.16, 
"max":15.49, 
"night":13.16, 
"eve":15.49, 
"morn":15.49 
}, 
"pressure":1015.77, 
"humidity":50, 
"weather":[ 
{ 
"id":802, 
"main":"Clouds", 
"description":"scattered clouds", 
"icon":"03n" 
} 
], 
"speed":4.47, 
"deg":116, 
"clouds":48 
}, 
{ 
"dt":1462618800, 
"temp":{ 
"day":23.03, 
"min":13.57, 
"max":23.03, 
"night":15.77, 
"eve":21.37, 
"morn":13.57 
}, 
"pressure":1015.43, 
"humidity":50, 
"weather":[ 
{ 
"id":803, 
"main":"Clouds", 
"description":"broken clouds", 
"icon":"04d" 
} 
], 
"speed":4.43, 
"deg":127, 
"clouds":56 
}, 
{ 
"dt":1462705200, 
"temp":{ 
"day":24.2, 
"min":16.14, 
"max":24.53, 
"night":18.32, 
"eve":23.58, 
"morn":16.14 
}, 
"pressure":1016.42, 
"humidity":42, 
"weather":[ 
{ 
"id":803, 
"main":"Clouds", 
"description":"broken clouds", 
"icon":"04d" 
} 
], 
"speed":7.22, 
"deg":121, 
"clouds":68 
}, 
{ 
"dt":1462791600, 
"temp":{ 
"day":23.4, 
"min":17.51, 
"max":23.59, 
"night":18.36, 
"eve":22.42, 
"morn":17.51 
}, 
"pressure":1017.75, 
"humidity":49, 
"weather":[ 
{ 
"id":800, 
"main":"Clear", 
"description":"clear sky", 
"icon":"02d" 
} 
], 
"speed":8.36, 
"deg":106, 
"clouds":8 
}, 
{ 
"dt":1462878000, 
"temp":{ 
"day":22.79, 
"min":16.59, 
"max":22.88, 
"night":17.15, 
"eve":21.54, 
"morn":16.59 
}, 
"pressure":1013.95, 
"humidity":50, 
"weather":[ 
{ 
"id":802, 
"main":"Clouds", 
"description":"scattered clouds", 
"icon":"03d" 
} 
], 
"speed":8.53, 
"deg":89, 
"clouds":36 
}, 
{ 
"dt":1462964400, 
"temp":{ 
"day":19.18, 
"min":13.92, 
"max":19.18, 
"night":14.57, 
"eve":18.88, 
"morn":13.92 
}, 
"pressure":1004.19, 
"humidity":0, 
"weather":[ 
{ 
"id":500, 
"main":"Rain", 
"description":"light rain", 
"icon":"10d" 
} 
], 
"speed":3.24, 
"deg":180, 
"clouds":22, 
"rain":2.13 
}, 
{ 
"dt":1463050800, 
"temp":{ 
"day":14.84, 
"min":8.83, 
"max":14.84, 
"night":8.83, 
"eve":12.92, 
"morn":12.53 
}, 
"pressure":1005.56, 
"humidity":0, 
"weather":[ 
{ 
"id":500, 
"main":"Rain", 
"description":"light rain", 
"icon":"10d" 
} 
], 
"speed":4.14, 
"deg":325, 
"clouds":73, 
"rain":2.86 
} 
] 
} 

申し訳ありませんが、ありがとう!

答えて

3

あなたは実際に次の6日間の情報を得ています。完全な配列を見て、あなたが完全な結果で見ることができるように

print_r($data); 

を行う例えば、6日のtheres:

[5] => Array 
      (
       [dt] => 1462964400 
       [temp] => Array 
        (
         [day] => 19.18 
         [min] => 13.92 
         [max] => 19.18 
         [night] => 14.57 
         [eve] => 18.88 
         [morn] => 13.92 
        ) 

       [pressure] => 1004.19 
       [humidity] => 0 
       [weather] => Array 
        (
         [0] => Array 
          (
           [id] => 500 
           [main] => Rain 
           [description] => light rain 
           [icon] => 10d 
          ) 

        ) 

       [speed] => 3.24 
       [deg] => 180 
       [clouds] => 22 
       [rain] => 2.13 
      ) 

だから、あなたが何をする必要があるかのforeach

foreach($data['list'] as $day => $value) { 
    echo "Max temperature for day " . $day . " will be " . $value[temp][max] . "<br />" ; 
} 
です

これが返されます:

Max temperature for day 0 will be 21.87 
Max temperature for day 1 will be 23.03 
Max temperature for day 2 will be 24.53 
Max temperature for day 3 will be 23.59 
Max temperature for day 4 will be 22.88 
Max temperature for day 5 will be 19.18 
Max temperature for day 6 will be 14.84 

これが役立つことを願っています!

+0

ありがとうございます!魅力のように動作します。 – user6302345

+0

あなたは大歓迎です! – eljamz

関連する問題