在这里浏览了许多其他问题之后,我仍然对此保持不变。首先,我从来没有使用过JSON数据,而是在找些容易而又棘手的东西。
我从DarkSky Weather API收到这样的JSON响应
{
"latitude": -26.202271,
"longitude": 28.043631,
"timezone": "Africa\/Johannesburg",
"daily": {
"summary": "Light rain throughout the week, with high temperatures peaking at 28\u00b0C on Saturday.",
"icon": "rain",
"data": [
{
"time": 1549317600,
"summary": "Mostly cloudy throughout the day.",
"icon": "partly-cloudy-day",
"sunriseTime": 1549338410,
"sunsetTime": 1549385960,
"moonPhase": 0.02,
"precipIntensity": 0.0737,
"precipIntensityMax": 0.6121,
"precipIntensityMaxTime": 1549368000,
"precipProbability": 0.14,
"precipType": "rain",
"temperatureHigh": 22.68,
"temperatureHighTime": 1549364400,
"temperatureLow": 15.14,
"temperatureLowTime": 1549422000,
"apparentTemperatureHigh": 22.68,
"apparentTemperatureHighTime": 1549364400,
"apparentTemperatureLow": 15.14,
"apparentTemperatureLowTime": 1549422000,
"dewPoint": 13.85,
"humidity": 0.74,
"pressure": 1015.08,
"windSpeed": 1.84,
"windGust": 7.01,
"windGustTime": 1549342800,
"windBearing": 40,
"cloudCover": 0.73,
"uvIndex": 8,
"uvIndexTime": 1549357200,
"visibility": 14.03,
"ozone": 263.92,
"temperatureMin": 15.02,
"temperatureMinTime": 1549339200,
"temperatureMax": 22.68,
"temperatureMaxTime": 1549364400,
"apparentTemperatureMin": 15.02,
"apparentTemperatureMinTime": 1549339200,
"apparentTemperatureMax": 22.68,
"apparentTemperatureMaxTime": 1549364400
},
{
"time": 1549404000,
"summary": "Mostly cloudy throughout the day.",
"icon": "partly-cloudy-day",
"sunriseTime": 1549424856,
"sunsetTime": 1549472325,
"moonPhase": 0.05,
"precipIntensity": 0.0432,
"precipIntensityMax": 0.2819,
"precipIntensityMaxTime": 1549454400,
"precipProbability": 0.13,
"precipType": "rain",
"temperatureHigh": 24.32,
"temperatureHighTime": 1549454400,
"temperatureLow": 15.83,
"temperatureLowTime": 1549508400,
"apparentTemperatureHigh": 24.32,
"apparentTemperatureHighTime": 1549454400,
"apparentTemperatureLow": 15.83,
"apparentTemperatureLowTime": 1549508400,
"dewPoint": 13.06,
"humidity": 0.67,
"pressure": 1013.86,
"windSpeed": 1.34,
"windGust": 6.01,
"windGustTime": 1549414800,
"windBearing": 341,
"cloudCover": 0.71,
"uvIndex": 8,
"uvIndexTime": 1549443600,
"visibility": 16.09,
"ozone": 258.39,
"temperatureMin": 15.14,
"temperatureMinTime": 1549422000,
"temperatureMax": 24.32,
"temperatureMaxTime": 1549454400,
"apparentTemperatureMin": 15.14,
"apparentTemperatureMinTime": 1549422000,
"apparentTemperatureMax": 24.32,
"apparentTemperatureMaxTime": 1549454400
},
{
"time": 1549490400,
"summary": "Light rain overnight.",
"icon": "rain",
"sunriseTime": 1549511300,
"sunsetTime": 1549558690,
"moonPhase": 0.08,
"precipIntensity": 0.16,
"precipIntensityMax": 0.6096,
"precipIntensityMaxTime": 1549573200,
"precipProbability": 0.23,
"precipType": "rain",
"temperatureHigh": 25.43,
"temperatureHighTime": 1549540800,
"temperatureLow": 16.47,
"temperatureLowTime": 1549591200,
"apparentTemperatureHigh": 25.43,
"apparentTemperatureHighTime": 1549540800,
"apparentTemperatureLow": 16.61,
"apparentTemperatureLowTime": 1549591200,
"dewPoint": 14.17,
"humidity": 0.69,
"pressure": 1013.06,
"windSpeed": 2.28,
"windGust": 7.57,
"windGustTime": 1549497600,
"windBearing": 342,
"cloudCover": 0.52,
"uvIndex": 12,
"uvIndexTime": 1549533600,
"visibility": 16.09,
"ozone": 251.95,
"temperatureMin": 15.83,
"temperatureMinTime": 1549508400,
"temperatureMax": 25.43,
"temperatureMaxTime": 1549540800,
"apparentTemperatureMin": 15.83,
"apparentTemperatureMinTime": 1549508400,
"apparentTemperatureMax": 25.43,
"apparentTemperatureMaxTime": 1549540800
},
{
"time": 1549576800,
"summary": "Mostly cloudy throughout the day.",
"icon": "partly-cloudy-day",
"sunriseTime": 1549597745,
"sunsetTime": 1549645053,
"moonPhase": 0.11,
"precipIntensity": 0.2819,
"precipIntensityMax": 1.1481,
"precipIntensityMaxTime": 1549584000,
"precipProbability": 0.39,
"precipType": "rain",
"temperatureHigh": 25.17,
"temperatureHighTime": 1549627200,
"temperatureLow": 16.64,
"temperatureLowTime": 1549677600,
"apparentTemperatureHigh": 25.19,
"apparentTemperatureHighTime": 1549627200,
"apparentTemperatureLow": 16.64,
"apparentTemperatureLowTime": 1549677600,
"dewPoint": 15.46,
"humidity": 0.77,
"pressure": 1013.3,
"windSpeed": 1.21,
"windGust": 5.91,
"windGustTime": 1549580400,
"windBearing": 310,
"cloudCover": 0.91,
"uvIndex": 7,
"uvIndexTime": 1549616400,
"visibility": 16.09,
"ozone": 249.44,
"temperatureMin": 16.47,
"temperatureMinTime": 1549591200,
"temperatureMax": 25.17,
"temperatureMaxTime": 1549627200,
"apparentTemperatureMin": 16.61,
"apparentTemperatureMinTime": 1549591200,
"apparentTemperatureMax": 25.19,
"apparentTemperatureMaxTime": 1549627200
},
{
"time": 1549663200,
"summary": "Rain overnight.",
"icon": "rain",
"sunriseTime": 1549684188,
"sunsetTime": 1549731414,
"moonPhase": 0.14,
"precipIntensity": 0.221,
"precipIntensityMax": 0.7645,
"precipIntensityMaxTime": 1549670400,
"precipProbability": 0.27,
"precipType": "rain",
"temperatureHigh": 27.92,
"temperatureHighTime": 1549713600,
"temperatureLow": 16.84,
"temperatureLowTime": 1549760400,
"apparentTemperatureHigh": 27.92,
"apparentTemperatureHighTime": 1549713600,
"apparentTemperatureLow": 16.93,
"apparentTemperatureLowTime": 1549760400,
"dewPoint": 14.08,
"humidity": 0.67,
"pressure": 1011.76,
"windSpeed": 0.19,
"windGust": 6.53,
"windGustTime": 1549688400,
"windBearing": 145,
"cloudCover": 0.94,
"uvIndex": 7,
"uvIndexTime": 1549702800,
"visibility": 16.09,
"ozone": 253.47,
"temperatureMin": 16.64,
"temperatureMinTime": 1549677600,
"temperatureMax": 27.92,
"temperatureMaxTime": 1549713600,
"apparentTemperatureMin": 16.64,
"apparentTemperatureMinTime": 1549677600,
"apparentTemperatureMax": 27.92,
"apparentTemperatureMaxTime": 1549713600
},
{
"time": 1549749600,
"summary": "Rain in the morning.",
"icon": "rain",
"sunriseTime": 1549770632,
"sunsetTime": 1549817775,
"moonPhase": 0.17,
"precipIntensity": 1.0668,
"precipIntensityMax": 4.2316,
"precipIntensityMaxTime": 1549760400,
"precipProbability": 0.43,
"precipType": "rain",
"temperatureHigh": 24.07,
"temperatureHighTime": 1549803600,
"temperatureLow": 17.25,
"temperatureLowTime": 1549854000,
"apparentTemperatureHigh": 24.07,
"apparentTemperatureHighTime": 1549803600,
"apparentTemperatureLow": 17.25,
"apparentTemperatureLowTime": 1549854000,
"dewPoint": 14.73,
"humidity": 0.72,
"pressure": 1011.87,
"windSpeed": 2.52,
"windGust": 7.58,
"windGustTime": 1549778400,
"windBearing": 358,
"cloudCover": 0.72,
"uvIndex": 14,
"uvIndexTime": 1549792800,
"visibility": 15.58,
"ozone": 257.27,
"temperatureMin": 16.84,
"temperatureMinTime": 1549760400,
"temperatureMax": 24.07,
"temperatureMaxTime": 1549803600,
"apparentTemperatureMin": 16.93,
"apparentTemperatureMinTime": 1549760400,
"apparentTemperatureMax": 24.07,
"apparentTemperatureMaxTime": 1549803600
},
{
"time": 1549836000,
"summary": "Mostly cloudy starting in the afternoon.",
"icon": "partly-cloudy-night",
"sunriseTime": 1549857075,
"sunsetTime": 1549904134,
"moonPhase": 0.2,
"precipIntensity": 0.0381,
"precipIntensityMax": 0.2489,
"precipIntensityMaxTime": 1549886400,
"precipProbability": 0.13,
"precipType": "rain",
"temperatureHigh": 26.19,
"temperatureHighTime": 1549890000,
"temperatureLow": 15.78,
"temperatureLowTime": 1549944000,
"apparentTemperatureHigh": 26.19,
"apparentTemperatureHighTime": 1549890000,
"apparentTemperatureLow": 15.78,
"apparentTemperatureLowTime": 1549944000,
"dewPoint": 12.99,
"humidity": 0.61,
"pressure": 1008.71,
"windSpeed": 2.54,
"windGust": 8.12,
"windGustTime": 1549857600,
"windBearing": 326,
"cloudCover": 0.47,
"uvIndex": 15,
"uvIndexTime": 1549879200,
"visibility": 16.09,
"ozone": 246.75,
"temperatureMin": 17.25,
"temperatureMinTime": 1549854000,
"temperatureMax": 26.19,
"temperatureMaxTime": 1549890000,
"apparentTemperatureMin": 17.25,
"apparentTemperatureMinTime": 1549854000,
"apparentTemperatureMax": 26.19,
"apparentTemperatureMaxTime": 1549890000
},
{
"time": 1549922400,
"summary": "Rain in the afternoon.",
"icon": "rain",
"sunriseTime": 1549943518,
"sunsetTime": 1549990493,
"moonPhase": 0.24,
"precipIntensity": 0.3912,
"precipIntensityMax": 1.6332,
"precipIntensityMaxTime": 1549972800,
"precipProbability": 0.48,
"precipType": "rain",
"temperatureHigh": 22.24,
"temperatureHighTime": 1549983600,
"temperatureLow": 16.23,
"temperatureLowTime": 1550030400,
"apparentTemperatureHigh": 22.24,
"apparentTemperatureHighTime": 1549983600,
"apparentTemperatureLow": 16.23,
"apparentTemperatureLowTime": 1550030400,
"dewPoint": 13.89,
"humidity": 0.72,
"pressure": 1008.8,
"windSpeed": 1.88,
"windGust": 8.13,
"windGustTime": 1549972800,
"windBearing": 1,
"cloudCover": 0.94,
"uvIndex": 6,
"uvIndexTime": 1549958400,
"visibility": 16.09,
"ozone": 244.47,
"temperatureMin": 15.78,
"temperatureMinTime": 1549944000,
"temperatureMax": 22.24,
"temperatureMaxTime": 1549983600,
"apparentTemperatureMin": 15.78,
"apparentTemperatureMinTime": 1549944000,
"apparentTemperatureMax": 22.24,
"apparentTemperatureMaxTime": 1549983600
}
]
},
"offset": 2
}
我现在想要实现的是在接下来的5天(今天或昨天除外)中提取temperatureHigh和temperatureLow。
根据上述代码从2019年2月5日起更新的内容,需要排除前两天。因此,我想开始从时间戳1549490400
我最后想要的是回显数据的以下显示。
Johannesburg
Wednesday [weathericon] MAX: `temperatureHigh` °C LOW: `temperatureLow` °C
Thursday [weathericon] MAX: `temperatureHigh` °C LOW: `temperatureLow` °C
Friday [weathericon] MAX: `temperatureHigh` °C LOW: `temperatureLow` °C
Saturday [weathericon] MAX: `temperatureHigh` °C LOW: `temperatureLow` °C
Sunday [weathericon] MAX: `temperatureHigh` °C LOW: `temperatureLow` °C
Monday [weathericon] MAX: `temperatureHigh` °C LOW: `temperatureLow` °C
我也可能只希望进行为期3天的预测
Johannesburg
Wednesday [weathericon] MAX: `temperatureHigh` °C LOW: `temperatureLow` °C
Thursday [weathericon] MAX: `temperatureHigh` °C LOW: `temperatureLow` °C
Friday [weathericon] MAX: `temperatureHigh` °C LOW: `temperatureLow` °C
在说[weathericon]的地方,我每天都需要拉"icon": "rain"
,并将其与本地存储的同名.jpg / .png匹配。即。 rain.png或rain.jpg
我尝试了各种循环,这里有其他答案的foreach示例,但是似乎没有任何东西可以使我正确显示数据。
这是我的部分工作,但是由于缺乏对JSON的理解,所以不确定为什么我会得到所有空白行。
<?php
header('Content-Type: application/json');
$json_string = file_get_contents("https://api.darksky.net/forecast/MYAPIKEY/-26.202271,28.043631?exclude=[currently,minutely,hourly,alerts,flags]&units=si");
$jsondata = json_decode($json_string, true);
//Echo Data
foreach ($jsondata as $max) {
echo "MAX: ".$max['data'][2]['temperatureHigh']."\n";
echo "MAX: ".$max['data'][3]['temperatureHigh']."\n";
echo "MAX: ".$max['data'][4]['temperatureHigh']."\n";
}
?>
上面给了我这个输出
MAX:
MAX:
MAX:
MAX:
MAX:
MAX:
MAX:
MAX:
MAX:
MAX: 25.43
MAX: 25.17
MAX: 27.92
MAX:
MAX:
MAX:
答案 0 :(得分:1)
您缺少正确的数组索引。继续:
foreach($jsondata['daily']['data'] as $dayData) {
$dayData['temperatureHigh'];
}