我正在使用以下代码从这个 JSON 中获取一些元素,但似乎没有任何效果。有人知道我做错了什么吗?(我花了很长时间试图弄清楚)。
$(document).ready(function() {
$.getJSON('http://free.worldweatheronline.com/feed/weather.ashx?q=Stockholm&format=json&num_of_days=2&key=e8536d3a52101433121710', function(Wdata) {
$.each(Wdata.data, function() {
$('<div id="test"></div>').append(
this.weather[0].date;
).appendTo('body');
});
});
});
我的 HTML 看起来像这样:
<!doctype html>
<html lang="se">
<head>
<meta charset="utf-8" />
<title>Title of This Web Page</title>
<script src="scripts\jquery-1.8.2.js" type="text/javascript"></script>
<script src="scripts\js.js" type="text/javascript"></script>
</head>
<body>
<div id="test">
</div>
</body>
</html>
JSON 看起来像这样:
{
"data": {
"current_condition": [ … ],
"request": [ … ],
"weather": [
{
"date": "2012-10-17",
"precipMM": "0.8",
"tempMaxC": "11",
"tempMaxF": "51",
"tempMinC": "8",
"tempMinF": "46",
"weatherCode": "119",
"weatherDesc": [
{
"value": "Cloudy"
}
],
"weatherIconUrl": [
{
"value": "http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0003_white_cloud.png"
}
],
"winddir16Point": "SW",
"winddirDegree": "224",
"winddirection": "SW",
"windspeedKmph": "27",
"windspeedMiles": "17"
},
{ … }
]
}
}