gpt4 book ai didi

arrays - 如何从 JSON 数组中提取结果并导入 ColdFusion 以获得 7 天的天气预报

转载 作者:行者123 更新时间:2023-12-01 22:57:30 26 4
gpt4 key购买 nike

我在这个网站上做了很多研究,所以如果我在发布此内容之前没有找到正确的答案,请原谅我。我特别尝试从天气 channel wundergound API 中提取数据,以便在我们的 ColdFusion 网站上显示 7-10 天的天气预报。我可以毫无问题地从 JSON 中提取不在 JSON 数组内的结果。

这是我用来显示单日天气结果的具体工作代码:

<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>

虽然上面的代码有效,但我不清楚如何从 JSON 数组中提取 7-10 天的天气预报并将其带入 ColdFusion。经过几天的研究,我还没有找到答案。我重新阅读了我的 ColdFusion 书,也没有找到答案。天气 channel 的支持和文档非常差,所以它们没有太大帮助。这是 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"
}
]
},

天气 channel wunderground 支持告诉我的唯一一件事是 forecastday 是我需要从中提取数据的数组。

我还能够获取 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_forecastsimpleForecast。两者均包含按日历顺序排列的 future 五 (5) 天的每日预测。

txt_forecast.forecastDay 每天包含两次预报:一次是早上和晚上。
txt_forecast.forecastDay dump

simpleForecast.forecastDay 包含每天的单个预报,以及高温和低温等具体详细信息。

simpleForecast.forecastDay dump

由于数组元素按日历顺序排列,因此只需循环遍历数组元素即可。例如,使用 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>

结果:

Simple Forecast Next 5 Days

使用与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>

Truncated txt_forecast results

显然这不是全部,但应该为您提供一个良好的起点。

关于arrays - 如何从 JSON 数组中提取结果并导入 ColdFusion 以获得 7 天的天气预报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37012391/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com