Javascript 无法检索json数据

Javascript 无法检索json数据,javascript,jquery,json,jsonp,Javascript,Jquery,Json,Jsonp,我正在尝试获取json数据,但我无法这样做。我正在尝试获取特定城市的天气数据。这是我的json数据 { "data": { "current_condition": [ { "cloudcover": "100", "humidity": "100", "observation_time": "01:07 PM", "precipMM": "0.2",

我正在尝试获取json数据,但我无法这样做。我正在尝试获取特定城市的天气数据。这是我的json数据

{ 
"data": 
{ 
    "current_condition": 
    [ 
        {
            "cloudcover": "100", 
            "humidity": "100", 
            "observation_time": "01:07 PM", 
            "precipMM": "0.2", 
            "pressure": "993", 
            "temp_C": "-6", 
            "temp_F": "21", 
            "visibility": "10", 
            "weatherCode": "368",  
            "weatherDesc": 
            [ 
                {
                    "value": "Light snow showers" 
                } 
            ],  
            "weatherIconUrl": 
            [ 
                {
                    "value": "http:\/\/www.worldweatheronline.com\/images\/wsymbols01_png_64\/wsymbol_0027_light_snow_showers_night.png" 
                } 
            ], 
            "winddir16Point": "N", 
            "winddirDegree": "360", 
            "windspeedKmph": "9", 
            "windspeedMiles": "6"
        } 
    ],  
    "request": 
    [ 
        {
            "query": "Tampere, Finland", 
            "type": "City" 
        } 
    ],  
    "weather": 
    [ 
        {
            "date": "2012-01-07", 
            "precipMM": "2.3", 
            "tempMaxC": "-4", 
            "tempMaxF": "25", 
            "tempMinC": "-8", 
            "tempMinF": "17", 
            "weatherCode": "326",  
            "weatherDesc": 
            [ 
                {
                    "value": "Light snow" 
                } 
            ],  
            "weatherIconUrl": 
            [ 
                {
                    "value": "http:\/\/www.worldweatheronline.com\/images\/wsymbols01_png_64\/wsymbol_0011_light_snow_showers.png" 
                } 
            ], 
            "winddir16Point": "NNW", 
            "winddirDegree": "336", 
            "winddirection": "NNW", 
            "windspeedKmph": "9", 
            "windspeedMiles": "5" 
        }, 
        {
            "date": 
            "2012-01-08", 
            "precipMM": "0.0", 
            "tempMaxC": "-7", 
            "tempMaxF": "19", 
            "tempMinC": "-9", 
            "tempMinF": "17", 
            "weatherCode": "116",  
            "weatherDesc": 
            [ 
                {
                    "value": "Partly Cloudy" 
                } 
            ],  
            "weatherIconUrl": 
            [ 
                {
                    "value": "http:\/\/www.worldweatheronline.com\/images\/wsymbols01_png_64\/wsymbol_0002_sunny_intervals.png" 
                } 
            ], 
            "winddir16Point": "SSE", 
            "winddirDegree": "148", 
            "winddirection": "SSE", 
            "windspeedKmph": "5", 
            "windspeedMiles": "3" 
        } 
    ] 
}
}
下面是我如何使用jquery获取它的

var container = $('.weatherContainer');
        var url = 'http://free.worldweatheronline.com/feed/weather.ashx?q=Tampere&format=json&num_of_days=2&key=a84523bbed133415120701&callback=?';
        $.getJSON(url, function(w) {
            //console.log(w.data);
            var contents = "<div class='c'>";
            $.each(w.data, function(i, res){
                //alert('h');
                $.each(res.weather, function(j,action) {
                    //alert('i');
                    contents += "<section>" + action.tempMaxC + "</section>";
                });
            });
            contents += "</div>";
            container.append(contents);
        });
我想得到天气物体内部的数据。我怎么能得到这个


好的,这是我取消注释console.log时输出的图像。您的问题是嵌套的“each”循环。您告诉jQuery响应的
数据
对象中的每个对象,找到这些对象中的每个
weather
对象并对其进行迭代。只有一个
天气
对象,它直接位于
数据
的内部,因此移除外部循环并使用此单个循环简化:

$.each(w.data.weather, function(i, res) {        
    contents += "<section>" + res.tempMaxC + "</section>";
});
$。每个(w.data.weather,function(i,res){
内容+=“”+res.tempMaxC+“”;
});

总而言之,你很接近。这是一个小改动。

您的问题是嵌套的“each”循环。您告诉jQuery响应的
数据
对象中的每个对象,找到这些对象中的每个
weather
对象并对其进行迭代。只有一个
天气
对象,它直接位于
数据
的内部,因此移除外部循环并使用此单个循环简化:

$.each(w.data.weather, function(i, res) {        
    contents += "<section>" + res.tempMaxC + "</section>";
});
$。每个(w.data.weather,function(i,res){
内容+=“”+res.tempMaxC+“”;
});

总而言之,你很接近。这是一个小改动。

“不起作用”不是问题分析。这甚至不是一个问题描述。当您运行代码时,实际会发生什么?控制台中是否有错误?我注意到你有一个注释掉的
console.log(w.data)
-如果你取消注释它会打印什么?好的,当我取消注释console.log(w.data)时,它给出了这个错误对象是未定义的[Break On this error]length=object.length。它显示这个对象{current_condition=[1],request=[1],weather=[2]}我编辑了我的帖子,我添加了错误。我不理解这个错误。请帮忙“不起作用”不是问题分析。这甚至不是一个问题描述。当您运行代码时,实际会发生什么?控制台中是否有错误?我注意到你有一个注释掉的
console.log(w.data)
-如果你取消注释它会打印什么?好的,当我取消注释console.log(w.data)时,它给出了这个错误对象是未定义的[Break On this error]length=object.length。它显示这个对象{current_condition=[1],request=[1],weather=[2]}我编辑了我的帖子,我添加了错误。我不理解这个错误。请帮忙