Javascript JQuery$.getJSON让我很困惑

Javascript JQuery$.getJSON让我很困惑,javascript,jquery,html,json,Javascript,Jquery,Html,Json,我完全被$.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.humidi

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

这一定与我的语法有关

尝试传递
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>');
});
$.getJSON('http://api.worldweatheronline.com/free/v1/weather.ashx?key=mykey&q=“+lat+”、“+longi+”&fx=no&format=json&callback=?”,函数(数据){
$(“#weather').html(“湿度:”+data.current#u condition.湿度+”

); $(“#weather”)。追加(“Temp:”+data.current_condition.Temp\u C+”

); $(“#天气”)。追加(“风:”+data.current_condition.windspeedMiles+”

); });
试试这个:

$.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>');
});
$.getJSON('http://api.worldweatheronline.com/free/v1/weather.ashx?key=mykey&q=“+lat+”、“+longi+”&fx=no&format=json&callback=?”,函数(数据){
$(“#weather').html(“湿度:”+data.data.current#u condition[0]。湿度+”

); $(“#weather”).append(“Temp:”+data.data.current_condition[0]。Temp_C+”

); $(“#weather”).append(“风:”+data.data.current_条件[0].风速表+”

); });

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

在JSON数据
当前_条件中
是一个对象数组,您可以使用相应的索引获取值,否则该值将未定义。尝试类似于<代码>数据.CurrutyByth[[ 0 ] ],而不是<代码> DATA CurrutyPosial.Wase。另一个要考虑的是,因为root是“代码> {数据”:YATA} /CODE >,您可能需要引用嵌套数据,如<代码> DATA .DATA。CurrutyBug条件[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>');
});