我试图建立一个小天气应用程序,我需要明天的主要天气状况。我已经搜索了StackOverflow几个小时,似乎无法让它工作。
{"cod":"200","message":0.0026,"city":{"id":3413829,"name":"Reykjavik","coord":
{"lon":-21.895411,"lat":64.135483},"country":"IS","population":0,"sys":
{"population":0}},"cnt":1,"list":[{"dt":1404349200,"temp":
{"day":281.58,"min":281.18,"max":283.73,"night":283.73,"eve":281.38,"morn":282.15},"pressure":971.54,"humidity":100
但这里是我正在寻找的数组中的部分。
,"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10d"}],"speed":2.52,"deg":308,"clouds":92,"rain":2.5}]}
这里是php:
$json_string = file_get_contents("http://api.openweathermap.org/data/2.5/forecast/daily?q=Reykjavik&cnt=1&mode=json");
$jsonData = json_decode($json_string, true);
$vedur = $jsonData["weather"]["main"];
echo "Það er ".$vedur . "í Reykjavík";
答案 0 :(得分:0)
可能是这样,你写了:
$jsonData["weather"]["main"][;
有一个额外的方括号应该是:
$jsonData["weather"]["main"];
答案 1 :(得分:0)
你需要首先遍历列表数组以获取(我猜)所有日子,然后遍历那里的项目以显示每天的天气:
foreach ($jsonData['list'] AS $item) {
foreach ($item['weather'] AS $weather) {
var_dump($item['weather']);
}
}
或您的示例:
foreach ($jsonData['list'] AS $item) {
foreach ($item['weather'] AS $weather) {
echo "Það er ".$weather['main'] . "í Reykjavík";
}
}
答案 2 :(得分:0)
$json_string = file_get_contents("http://api.openweathermap.org/data/2.5/forecast/daily?q=Reykjavik&cnt=1&mode=json");
$jsonData = json_decode($json_string, true);
$vedur = $jsonData['list'][0]['weather'][0]['main'];
echo "Það er ".$vedur . "í Reykjavík";