【问题标题】:Weather Underground API - Forcast Help - JavascriptWeather Underground API - 预测帮助 - Javascript
【发布时间】:2013-01-31 21:04:43
【问题描述】:

我正在尝试使用以下方法从 Weather Underground API 获取 3 天的预报:

var Forcast = parsed_json['forecast']['txt_forecast']['period'][0]['fcttext_metric'];

它不工作,我没有使用 json 的经验,所以我不知道我需要做什么才能让它工作。 Bellow是我想要的json文件的一部分:

"forecast": {
"txt_forecast": {
"date": "2:00 PM PDT",
"forecastday": [{
"period": 0,
"icon": "partlycloudy",
"icon_url": "http://icons-ak.wxug.com/i/c/k/partlycloudy.gif",
"title": "Tuesday",
"fcttext": "Partly cloudy in the morning, then clear. High of 68F. Breezy. Winds from the West at 10 to 25 mph.",
"fcttext_metric": "Partly cloudy in the morning, then clear. High of 20C. Windy. Winds from the West at 20 to 35 km/h.",
"pop": "0"
}, {

谁能告诉我如何获取我需要的信息。

【问题讨论】:

  • 您遇到了什么问题?
  • 如果我使用我说我试过的代码..我什么也没得到:|如果我只使用这个var Forcast = parsed_json['forecast']['fcttext_metric']; 然后其他命令工作但我没有得到任何预测信息

标签: javascript json api weather-api


【解决方案1】:

在 Wunderground 官方论坛上的一位用户的帮助下,我设法解决了这个问题。这是我正在使用的新代码,还有一些添加的位。

jQuery('#GetWeather').click(function() {
var PostCode=" ";
$.ajax({ url : "http://api.wunderground.com/api/2508132ae0c7601a/geolookup/forecast/q/UK/"+PostCode +".json",
dataType : "jsonp",
success : function(parsed_json) {

var Forcast = parsed_json['forecast']['simpleforecast']['forecastday'];

for (index in Forcast) {
var imageurl = "http://.....";
/* Weather Box 0 Day 1, 1st period */
$('.Wicon').css('background-image',"url("+imageurl+Forcast[0]['icon']+".svg)");
$('#GetWeatherDay').html(Forcast[0]['date']['weekday']);
$('#GetWeatherState').html(Forcast[0]['conditions']);
$('#GetWeatherTempHigh').html('High of '+Forcast[0]['high']['celsius']+'&#8451');
$('#GetWeatherTempLow').html('Low of '+Forcast[0]['low']['celsius']+'&#8451');
$('#GetWeatherMaxWind').html('Max Wind '+Forcast[0]['maxwind']['mph']+' Mph');
$('#GetWeatherAveWind').html('Average Wind '+Forcast[0]['avewind']['mph']+' Mph');
$('#GetWeatherWindDir').html('Wind Direction '+Forcast[0]['avewind']['dir']);
$('#GetWeatherHumidityA').html('Average '+Forcast[0]['avehumidity']+'%');

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-01
    • 2017-02-24
    • 2013-08-29
    • 2013-01-27
    相关资源
    最近更新 更多