我是Json的新手,并且一段时间没有接触过PHP。我正在使用json weather api在网页上显示当前的天气状况。我现在也决定添加一个预测。预测是在相同的jason文件中提供的,但格式略有不同,我无法获取我的PHP代码来提取它。我认为这是因为预测是一个当前条件不是,但仍然没有Joy的数组。
json文件的片段:
{
"response": {
"version":"0.1",
"termsofService":"http://www.wunderground.com/weather/api/d/terms.html",
"features": {
"geolookup": 1
,
"conditions": 1
,
"forecast": 1
}
},
"observation_location": {
"full":"Clifton, Bristol, ",
"city":"Clifton, Bristol",
"state":"",
"country":"UNITED KINGDOM",
"country_iso3166":"GB",
"latitude":"51.464230",
"longitude":"-2.616240",
"elevation":"230 ft"
},
"estimated": {
},
"station_id":"IBRISTOL22",
"observation_time":"Last Updated on June 6, 8:34 AM BST",
"observation_time_rfc822":"Fri, 06 Jun 2014 08:34:16 +0100",
"observation_epoch":"1402040056",
"local_time_rfc822":"Fri, 06 Jun 2014 08:38:21 +0100",
"feelslike_string":"56.7 F (13.7 C)",
"feelslike_f":"56.7",
"feelslike_c":"13.7",
"visibility_mi":"N/A",
"visibility_km":"N/A",
"solarradiation":"246",
"UV":"1.1","precip_1hr_string":"0.00 in ( 0 mm)",
"precip_1hr_in":"0.00",
"precip_1hr_metric":" 0",
"precip_today_string":"0.00 in (0 mm)",
"precip_today_in":"0.00",
"precip_today_metric":"0",
"icon":"clear",
"icon_url":"http://icons.wxug.com/i/c/k/clear.gif",
"forecast_url":"http://www.wunderground.com/global/stations/03726.html",
"history_url":"http://www.wunderground.com/weatherstation/WXDailyHistory.asp?ID=IBRISTOL22",
"ob_url":"http://www.wunderground.com/cgi-bin/findweather/getForecast?query=51.464230,-2.616240"
}
,
"forecast":{
"txt_forecast": {
"date":"7:36 AM BST",
"forecastday": [
{
"period":0,
"icon":"partlycloudy",
"icon_url":"http://icons.wxug.com/i/c/k/partlycloudy.gif",
"title":"Friday",
"fcttext":"Partly to mostly cloudy. High 74F. Winds ESE at 10 to 20 mph.",
"fcttext_metric":"Mostly cloudy skies early, then partly cloudy this afternoon. High 23C. Winds ESE at 15 to 30 kph.",
"pop":"10"
}
,
{
"period":1,
"icon":"nt_rain",
"icon_url":"http://icons.wxug.com/i/c/k/nt_rain.gif",
"title":"Friday Night",
"fcttext":"Partly cloudy skies early then becoming cloudy with occasional rain and a rumble or two of thunder late. Low around 60F. Winds ESE at 10 to 15 mph. Chance of rain 90%.",
"fcttext_metric":"Partly cloudy skies this evening will give way to periods of rain and possibly a thunderstorm overnight. Low 15C. Winds ESE at 15 to 25 kph. Chance of rain 90%.",
"pop":"90"
}
,
{
"period":2,
"icon":"rain",
"icon_url":"http://icons.wxug.com/i/c/k/rain.gif",
"title":"Saturday",
"fcttext":"Becoming partly cloudy after some morning rain. High around 70F. Winds SSW at 10 to 20 mph. Chance of rain 90%. Rainfall around a quarter of an inch.",
"fcttext_metric":"Rain early. A mix of sun and clouds in the afternoon. High 21C. Winds SSW at 15 to 25 kph. Chance of rain 90%. Rainfall around 6mm.",
"pop":"90"
}
,
{
"period":3,........
我已设法使用以下代码提取条件部分。
$json_string = file_get_contents("http://api.wunderground.com/api/XXXXXAPI-CodeXXXXX/geolookup/conditions/forecast/q/pws:ibristol22.json");
$parsed_json = json_decode($json_string);
$feelslike = $parsed_json->{'current_observation'}->{'feelslike_c'};
echo "<strong>Feels Like:</strong> ${feelslike} <br>";
我需要说什么PHP代码从'forecast'的句号'0'中提取'fcttext_metric' - &gt; 'forecastday'
答案 0 :(得分:0)
循环通过$parsed_json->forecast->txt_forecast->forecastday
:
foreach ($parsed_json->forecast->txt_forecast->forecastday as $forecastday)
{
if ($forecastday->period == 0)
{
echo('fcttext_metric=' . $forecastday->fcttext_metric);
// ...(do something else)...
}
}
或者,如果输出数据的顺序总是相同,则更简单:
echo('fcttext_metric=' . $parsed_json->forecast->txt_forecast->forecastday[0]->fcttext_metric);
假设period: 0
的数据始终位于forecastday