OpenWeather JSONP

时间:2014-01-26 01:24:23

标签: javascript jquery json

我正在使用他们的API访问OpenWeather数据,但尚未成功运行。我认为我正在错误地访问JSON但没有破解它。

我使用以下代码JSFiddle here)来查询他们的数据:

function getWeather(lat, lon, callback) {
    var weather = 'http://api.openweathermap.org/data/2.5/weather?lat=' + lat + '&lon=' + lon + '&cnt=1';
    $.ajax({
        dataType: "jsonp",
        url: weather,
        success: callback
    });
};

我运行的查询返回:

{
 "coord":
   {
    "lon":-6.27,"lat":13.34
   },
 "sys": 
   {
    "message":0.0088,
    "country":"ML",
    "sunrise":1390719134,
    "sunset":1390760592
   },
 "weather":
 [
   {
    "id":800,"main":"Clear",
    "description":"Sky is Clear",
    "icon":"01n"
   }
 ],
 "base":"cmc stations",
 "main": 
   {"temp":293.154,
    "temp_min":293.154,
    "temp_max":293.154,
    "pressure":989.21,
    "sea_level":1024.43,
    "grnd_level":989.21,
    "humidity":64
   },
 "wind":
   {
    "speed":4.1,
    "deg":52.0018
   },
 "clouds":
   {
    "all":0
   },
 "dt":1390695239,
 "id":2451478,
 "name":"Segou",
 "cod":200
}

然后我尝试格式化他们的响应:

var lat = 13.3428;
var lon = -6.26612;

    getWeather(lat, lon, function (data) {
        var tempHTML = "";
        tempHTML = tempHTML + '<h3>WEATHER INFO:</h3>';
        tempHTML = tempHTML + 'Weather data received <br/>';

        tempHTML = tempHTML + '<h3>WEATHER:</h3>';
        tempHTML = tempHTML + 'Weather Id: ' + data.weather[0].id + '<br/>';
        tempHTML = tempHTML + 'Weather Main: ' + data.weather[0].main + '<br/>';
        tempHTML = tempHTML + 'Weather Description: ' + data.weather[0].description + '<br/>';
        tempHTML = tempHTML + 'Weather Icon: ' + data.weather[0].icon + '<br/>';

        tempHTML = tempHTML + '<h3>MAIN:</h3>';
        tempHTML = tempHTML + 'Temperature: ' + data.main[0].temp + 'degrees<br/>';
        tempHTML = tempHTML + 'Temperature Min: ' + data.main[0].temp_min + 'degrees<br/>';
        tempHTML = tempHTML + 'Temperature Max: ' + data.main[0].temp_max + 'degrees<br/>';
        tempHTML = tempHTML + 'Pressure: ' + data.main[0].pressure + 'Kpa<br/>';
        tempHTML = tempHTML + 'Humidity: ' + data.main[0].humidity + '%<br/>';

        tempHTML = tempHTML + '<h3>WIND:</h3>';
        tempHTML = tempHTML + 'Wind Speed: ' + data.wind[0].speed + 'kmh<br/>';
        tempHTML = tempHTML + 'Wind Direction: ' + data.wind[0].deg + 'degrees <br/>';

        tempHTML = tempHTML + '<h3>CLOUDS:</h3>';
        tempHTML = tempHTML + 'Cloud Coverage: ' + data.clouds[0].all + '%<br/>';

        document.getElementById('weather_output').innerHTM = tempHTML;
    });

如果您查看我的JSFiddle,我会收到以下控制台错误:

Uncaught SyntaxError: Unexpected token : api.openweathermap.org/data/2.5/weather?lat=13.3428&lon=-6.26612&cnt=1&callback=jQuery191009959305939264596_1390699334749&_=1390699334750:1
Uncaught TypeError: Cannot read property 'temp' of undefined (index):36

1 个答案:

答案 0 :(得分:0)

如果查看返回的json,main键指向一个对象而不是数组,因此使用[0]会导致错误。

使用data.main.temp代替..

windsyscloud属性也是如此。

最后,你最后错过innerHTML中的最后一个L ..

演示http://jsfiddle.net/KLtLD/17/

处的所有修正