gpt4 book ai didi

jquery - 使用 jQuery 从 Wunderground 解析 JSONP

转载 作者:行者123 更新时间:2023-12-01 04:53:39 25 4
gpt4 key购买 nike

我在使用 JavaScript 和 Ajax 解析“wunderground”API 时遇到问题。我可以获得一些值,有些则不能。这是我使用的 API 的链接:

{
"forecast":{
"simpleforecast":{
"forecastday":[
{
"date":{
"epoch":"1369429200",
"pretty":"11:00 PM CEST on May 24, 2013",
"day":24,
"month":5,
"year":2013,
"yday":143,
"hour":23,
"min":"00",
"sec":0,
"isdst":"1",
"monthname":"May",
"weekday_short":"Fri",
"weekday":"Friday",
"ampm":"PM",
"tz_short":"CEST",
"tz_long":"Europe/Ljubljana"
},
"period":1,
"high":{
"fahrenheit":"63",
"celsius":"17"
},
"low":{
"fahrenheit":"45",
"celsius":"7"
},
"conditions":"Rain",
"icon":"rain",
"icon_url":"http://icons-ak.wxug.com/i/c/k/rain.gif",
"skyicon":"cloudy",
"pop":90,
"qpf_allday":{
"in":0.47,
"mm":11.9
},
"qpf_day":{
"in":0.38,
"mm":9.7
},
"qpf_night":{
"in":0.05,
"mm":1.3
},
"snow_allday":{
"in":0,
"cm":0
},
"snow_day":{
"in":0,
"cm":0
},
"snow_night":{
"in":0,
"cm":0
},
"maxwind":{
"mph":4,
"kph":6,
"dir":"West",
"degrees":270
},
"avewind":{
"mph":2,
"kph":3,
"dir":"SSW",
"degrees":204
},
"avehumidity":62,
"maxhumidity":72,
"minhumidity":57
}
]
}
}
}

我想将值 17 解析为摄氏度。到目前为止我有这个:

jQuery(document).ready(function($) {
$.ajax({
url : "http://api.wunderground.com/api/XXXXXXXXXX/geolookup/conditions/forecast/q/Slovenia/Maribor.json",
dataType : "jsonp",
success : function(parsed_json) {
var forecast_1 = parsed_json['forecast']['simpleforecast']['forecastday']['celsius'];
document.getElementById("forecast_1").innerHTML = forecast_1;
}
});
});
</script>

我都得到了“未定义”值。请帮助?谢谢

最佳答案

属性day是一个对象数组,因此您必须首先选择数组索引。此外,它没有直接属性celsius。但是,highlow 下有两个 celsius 属性。所以你必须决定要解决哪个问题。

总而言之,结果可能如下所示:

var forecast_1 = parsed_json['forecast']['simpleforecast']['forecastday'][0]['high']['celsius'];

此外,正确的输出格式有助于理解它。使用,例如 http://jsonformatter.curiousconcept.com/ .

关于jquery - 使用 jQuery 从 Wunderground 解析 JSONP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16737694/

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