我完全被$ .getJSON函數搞糊塗了!
$.getJSON('http://api.worldweatheronline.com/free/v1/weather.ashx?key=mykey&q=' + lat + ',' + longi + '&fx=no&format=json', function(data) {
$('#weather').html('<p> Humidity: ' + data.current_condition.humidity + '</p>');
$('#weather').append('<p>Temp : ' + data.current_condition.temp_C + '</p>');
$('#weather').append('<p> Wind: ' + data.current_condition.windspeedMiles + '</p>');
});
這是該網址JSON是:
{
"data":{
"current_condition":[
{
"cloudcover":"0",
"humidity":"82",
"observation_time":"04:07 PM",
"precipMM":"0.2",
"pressure":"997",
"temp_C":"11",
"temp_F":"52",
"visibility":"10",
"weatherCode":"356",
"weatherDesc":[
{
"value":"Moderate or heavy rain shower"
}
],
"weatherIconUrl":[
{
"value":"http:\/\/cdn.worldweatheronline.net\/images\/wsymbols01_png_64\/wsymbol_0010_heavy_rain_showers.png"
}
],
"winddir16Point":"WSW",
"winddirDegree":"240",
"windspeedKmph":"26",
"windspeedMiles":"16"
}
],
"request":[
{
"query":"Lat 51.24 and Lon -1.15",
"type":"LatLon"
}
]
}
}
它必須是與我的語法!
這不是JSONP。 – SLaks
在您的JSON數據中'current_condition'是一個對象數組,您可以使用相應的索引來獲取值,否則該值將是未定義的。 嘗試類似'data.current_condition [0] .humidity'而不是'data.current_condition.humidity'。 – super
要考慮的另一件事是,因爲根是'{'data':yatta}''你可能需要引用像'data.data.current_condition [0]' –