0

我完全被 $.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>');
});

这是该 url 处的 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"
         }
      ]
   }
}

这一定与我的语法有关!

4

2 回答 2

1

尝试callback=?作为使用 jsonp 格式的回调函数传递

$.getJSON('http://api.worldweatheronline.com/free/v1/weather.ashx?key=mykey&q=' + lat + ',' + longi + '&fx=no&format=json&callback=?', 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>');
});
于 2013-10-28T16:21:32.877 回答
0

试试这个 :

$.getJSON('http://api.worldweatheronline.com/free/v1/weather.ashx?key=mykey&q=' + lat + ',' + longi + '&fx=no&format=json&callback=?', function (data) {
    $('#weather').html('<p> Humidity: ' + data.data.current_condition[0].humidity + '</p>');
    $('#weather').append('<p>Temp : ' + data.data.current_condition[0].temp_C + '</p>');
    $('#weather').append('<p> Wind: ' + data.data.current_condition[0].windspeedMiles + '</p>');
});

因为current_condition是一个对象数组,您可以使用它的索引来访问它。添加数据属性,因为您的 JSON 本身包装了数据对象。

于 2013-10-28T16:26:00.153 回答