数组 – 如何拉出JSON数组和INTO ColdFusion的结果,进行7天的天气预报

我在这个网站上做了很多研究,如果在发布之前我没有找到正确的答案,请原谅我.我特意试图从天气频道wundergound API中提取数据,以便在我们的ColdFusion网站上显示7-10天的天气预报.我没有问题从
JSON中提取不在
JSON数组中的结果.

以下是我用于显示单日天气结果的具体WORKING代码:

<cfhttp url="http://api.wunderground.com/api/02e63b13f45e6430/forecast/geolookup/conditions/q/CA/San_Francisco.json">
<cfset parsed_json = deserializeJSON(cfhttp.fileContent)>
<cfset location = parsed_json.location.city>
<cfset weather = parsed_json.current_observation.weather>
<cfset temp_f = parsed_json.current_observation.temp_f>

<cfoutput>
<div class="weather-info">
<span class="current-conditions">Current Conditions</span> 
<span class="weather-location">#location#</span>
<center>#weather#</center>
<span class="weather-temp-f">#temp_f# <sup>&deg;</sup></span>
</cfoutput>

虽然上面的代码工作正常,但我不清楚如何将7-10天的天气预报从JSON阵列中拉出来并将其带入ColdFusion.经过几天的研究,我无法找到答案.我重新阅读了我的ColdFusion书,并没有找到答案.天气频道支持和文档非常差,所以他们不是很有帮助.这是JSON文件/数组:

http://api.wunderground.com/api/02e63b13f45e6430/forecast/geolookup/conditions/q/CA/San_Francisco.json

以下是该阵列的部分屏幕截图的链接:http://suiteamerica.com/images/JSON_array.jpg

这是JSON文件代码的一部分,它具有数组forecastday

"forecast":{
        "txt_forecast": {
        "date":"10:01 AM PDT",
        "forecastday": [
        {
        "period":0,
        "icon":"cloudy",
        "icon_url":"http://icons.wxug.com/i/c/k/cloudy.gif",
        "title":"Tuesday",
        "fcttext":"Cloudy. Slight chance of a rain shower. High 64F. Winds W at 10 to 15 mph.",
        "fcttext_metric":"Cloudy skies. Slight chance of a rain shower. High 18C. Winds W at 15 to 25 km/h.",
        "pop":"20"
        }
        ,
        {
        "period":1,
        "icon":"nt_cloudy",
        "icon_url":"http://icons.wxug.com/i/c/k/nt_cloudy.gif",
        "title":"Tuesday Night",
        "fcttext":"Cloudy. Slight chance of a rain shower. Low 52F. Winds SW at 10 to 15 mph.",
        "fcttext_metric":"Cloudy. Slight chance of a rain shower. Low 11C. Winds WSW at 15 to 25 km/h.",
        "pop":"20"
        }
        ,
        {
        "period":2,
        "icon":"mostlycloudy",
        "icon_url":"http://icons.wxug.com/i/c/k/mostlycloudy.gif",
        "title":"Wednesday",
        "fcttext":"Mostly cloudy skies. High 63F. Winds SW at 10 to 20 mph.",
        "fcttext_metric":"Mostly cloudy. High 17C. Winds SW at 15 to 30 km/h.",
        "pop":"20"
        }
        ,
        {
        "period":3,
        "icon":"nt_mostlycloudy",
        "icon_url":"http://icons.wxug.com/i/c/k/nt_mostlycloudy.gif",
        "title":"Wednesday Night",
        "fcttext":"Partly cloudy during the evening followed by cloudy skies overnight. Slight chance of a rain shower. Low 53F. Winds SW at 10 to 20 mph.",
        "fcttext_metric":"Partly cloudy skies during the evening will give way to cloudy skies overnight. Slight chance of a rain shower. Low 11C. Winds SW at 15 to 30 km/h.",
        "pop":"20"
        }
        ,
        {
        "period":4,
        "icon":"partlycloudy",
        "icon_url":"http://icons.wxug.com/i/c/k/partlycloudy.gif",
        "title":"Thursday",
        "fcttext":"Cloudy skies early, followed by partial clearing. High 64F. Winds WSW at 10 to 20 mph.",
        "fcttext_metric":"Cloudy early with partial sunshine expected late. High 17C. Winds WSW at 15 to 30 km/h.",
        "pop":"20"
        }
        ,
        {
        "period":5,
        "icon":"nt_chancerain",
        "icon_url":"http://icons.wxug.com/i/c/k/nt_chancerain.gif",
        "title":"Thursday Night",
        "fcttext":"Partly cloudy skies early followed by mostly cloudy skies and a few showers later at night. Low 52F. Winds WSW at 10 to 15 mph. Chance of rain 30%.",
        "fcttext_metric":"Partly cloudy during the evening. A few showers developing later during the night. Low 11C. Winds WSW at 15 to 30 km/h. Chance of rain 30%.",
        "pop":"30"
        }
        ,
        {
        "period":6,
        "icon":"chancerain",
        "icon_url":"http://icons.wxug.com/i/c/k/chancerain.gif",
        "title":"Friday",
        "fcttext":"Rain showers in the morning becoming more intermittent in the afternoon. High 63F. Winds SW at 10 to 15 mph. Chance of rain 50%.",
        "fcttext_metric":"Showers early becoming less numerous later in the day. High 17C. Winds SW at 15 to 25 km/h. Chance of rain 50%.",
        "pop":"50"
        }
        ,
        {
        "period":7,
        "icon":"nt_mostlycloudy",
        "icon_url":"http://icons.wxug.com/i/c/k/nt_mostlycloudy.gif",
        "title":"Friday Night",
        "fcttext":"Partly cloudy skies during the evening will give way to cloudy skies overnight. Low 54F. Winds SSW at 5 to 10 mph.",
        "fcttext_metric":"Partly cloudy during the evening followed by cloudy skies overnight. Low 12C. Winds SSW at 10 to 15 km/h.",
        "pop":"20"
        }
        ]
        },

天气预报wunderground支持唯一告诉我的是,预测日是我需要从中提取数据的数组.

我还能够获取DATE数据以显示位于Forecastday数组之前的那些数据:

<cfset fctdate = parsed_json.forecast.txt_forecast.date>

这是我真的不知道从JSON阵列中获取7-10天预测数据的最佳方法.

我是否必须从JSON数组中选择一个单独的结果,并在CFSET标记内定义它,例如Forecastday period [0]等…?或者我是否需要使用CFLOOP标签来获取结果?如果是这样,我怎么写CFLOOP来获取7-10天的天气预报呢?

任何帮助,将不胜感激.我想我已经提供了所有必要的文件来帮助解决这个问题.如果需要进一步澄清或者我错过了提供任何必要的文件,请告诉我.感谢所有帮助的人.

最佳答案 我不熟悉API,所以我开始转储反序列化的数据并寻找模式.转储显示JSON包含两个预测数组:txt_forecast和simpleForecast.两者都按日历顺序包含接下来五(5)天的每日预测.

txt_forecast.forecastDay每天包含两个预测:一个用于早晚.
《数组 – 如何拉出JSON数组和INTO ColdFusion的结果,进行7天的天气预报》

simpleForecast.forecastDay每天包含一个预测,具有特定的详细信息,如高温和低温.

《数组 – 如何拉出JSON数组和INTO ColdFusion的结果,进行7天的天气预报》

由于数组元素按日历顺序排列,因此只需遍历数组元素即可.例如,使用simpleForecast:

<cfset nextFiveDays = parsed_json.forecast.simpleForecast.forecastDay>
<cfoutput>
<cfloop array="#nextFiveDays#" index="forecast">
    <!--- Display day of week / date --->
    #forecast.date.weekday# / #forecast.date.monthName# #forecast.date.day#, #forecast.date.year# <br>
    <!--- Display conditions --->
    Conditions: #forecast.conditions#<br>
    Hi: #forecast.high.fahrenheit# F<br>
    Low: #forecast.low.fahrenheit# F<br>
    <img src="#forecast.icon_url#"><hr>
</cfloop>
</cfoutput>

结果:

《数组 – 如何拉出JSON数组和INTO ColdFusion的结果,进行7天的天气预报》

使用与txt_forecast相同类型的数组循环.只需插入相应的密钥名称:

<cfset nextFiveDays = parsed_json.forecast.txt_forecast.forecastDay>
<cfoutput>
<cfloop array="#nextFiveDays#" index="forecast">
    <!--- day of week --->
    #forecast.title# <br>
    Forecast: #forecast.fcttext#
    ... other values here ... 
</cfloop>
</cfoutput>

《数组 – 如何拉出JSON数组和INTO ColdFusion的结果,进行7天的天气预报》

显然这不是全局,但应该给你一个很好的起点.

点赞