我在解析来自api.world.weather.com的json请求时遇到问题,下面是我正在使用的代码。当我在控制台上查看JSON时,看起来我正在调用正确的数据,但我得到了错误。例如:ReferenceError:未定义天气
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Temp</title>
<style>
html {margin:2em; font-size:2em; font-family:Helvetica, Arial, sans-serif;}
h1 {margin:0 0 0.2em; color:#369;}
img {float:left; margin-right:1em;}
ul {float:left; margin:0; padding:0; list-style:none;}
</style>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
</head>
<body>
<section>
<h1>Current Weather for </h1>
<div class="img" id="data-img"></div>
<ul>
<li><b>Conditions:</b> <span id="data-current"></span></li>
<li><b>Temperature:</b> <span id="data-temp"></span>°</li>
<li><b>Humidity:</b> <span id="data-humidity"></span>%</li>
<li><b>Wind Speed:</b> <span id="data-wind"></span>mph</li>
<li id="zip"></li>
<li id="country"></li>
<li id="api"></li>
</ul>
</section>
<script>
var zipcode = '27560';
var appid = 'XXXXXXXXXXXXXXXXXXXXXX';
var startdate = '2015-10-01';
var enddate = '2015-10-28';
$.getJSON('http://api.worldweatheronline.com/premium/v1/past-weather.ashx?q='+ zipcode +'&format=json&date='+ startdate +'&enddate='+ enddate +'&key='+ appid, function(data){
console.log(data);
$("#data-current").text(data.weather[0].date);
$("#data-temp").text(data.hourly[0].tempF);
$("#data-humidity").text(data.hourly[0].humidity);
$("#data-wind").text(data.hourly[0].windspeedMiles);
})
.fail(function(jqxhr, textStatus, error) {
console.log("Request Failed" + textStatus + "," + error);
});
</script>
</body>
</html>
答案 0 :(得分:1)
从此JSON流返回的JSON具有以下结构:
{
data: {
request: [],
weather: [
hourly: []
],
}
}
这意味着您从getJSON
调用返回的对象上有一个数据属性,但您尝试访问weather
属性只有data
属性$("#data-current").text(data.data.weather[0].date);
$("#data-temp").text(data.data.weather[0].hourly[0].tempF);
$("#data-humidity").text(data.data.weather[0].hourly[0].humidity);
$("#data-wind").text(data.data.weather[0].hourly[0].windspeedMiles);
属性1}}。
您必须将代码更改为:
{{1}}
答案 1 :(得分:1)
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Temp</title>
<style>
html {margin:2em; font-size:2em; font-family:Helvetica, Arial, sans-serif;}
h1 {margin:0 0 0.2em; color:#369;}
img {float:left; margin-right:1em;}
ul {float:left; margin:0; padding:0; list-style:none;}
</style>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
</head>
<body>
<section>
<h1>Current Weather for </h1>
<div class="img" id="data-img"></div>
<ul>
<li><b>Conditions:</b> <span id="data-current"></span></li>
<li><b>Temperature:</b> <span id="data-temp"></span>°</li>
<li><b>Humidity:</b> <span id="data-humidity"></span>%</li>
<li><b>Wind Speed:</b> <span id="data-wind"></span>mph</li>
<li id="zip"></li>
<li id="country"></li>
<li id="api"></li>
</ul>
</section>
<script>
var zipcode = '27560';
var appid = '96afa96cadeb7165258ae95b77fdc';
var startdate = '2015-10-01';
var enddate = '2015-10-28';
$.getJSON('http://api.worldweatheronline.com/premium/v1/past-weather.ashx?q='+ zipcode +'&format=json&date='+ startdate +'&enddate='+ enddate +'&key='+ appid, function(data){
var data = data.data;
$("#data-current").text(data.weather[0].date);
$("#data-temp").text(data.hourly[0].tempF);
$("#data-humidity").text(data.hourly[0].humidity);
$("#data-wind").text(data.hourly[0].windspeedMiles);
})
.fail(function(jqxhr, textStatus, error) {
console.log("Request Failed" + textStatus + "," + error);
});
</script>
</body>
</html>
答案 2 :(得分:0)
您的数据是data.data;
并且您没有正确读取索引。将脚本更改为:
<script>
var zipcode = '27560';
var appid = '96afa96cadeb7165258ae95b77fdc';
var startdate = '2015-10-01';
var enddate = '2015-10-28';
$.getJSON('http://api.worldweatheronline.com/premium/v1/past-weather.ashx?q='+ zipcode +'&format=json&date='+ startdate +'&enddate='+ enddate +'&key='+ appid, function(data){
var newdata=data.data;
console.log(newdata);
$("#data-current").text(newdata.weather[0].date);
$("#data-temp").text(newdata.weather[0].hourly[0].tempF);
$("#data-humidity").text(newdata.weather[0].hourly[0].humidity);
$("#data-wind").text(newdata.weather[0].hourly[0].windspeedMiles);
})
.fail(function(jqxhr, textStatus, error) {
console.log("Request Failed" + textStatus + "," + error);
});
</script>