无法检索 json 数据

发布于 2024-12-25 10:55:50 字数 3862 浏览 0 评论 0原文

我正在尝试获取 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);
        });

Help 获取它的方法。我怎样才能实现它?上面的代码对我不起作用。

这里的错误是什么

object is undefined
length = object.length,                                   jquery-latest.js (line 630) 

我想获取天气对象内的数据。我怎样才能得到它?

好的,这是我取消注释 console.log 在此处输入链接描述时的输出图像

I am trying to get json data but I am not able to do so. I am trying to get weather data for a specific city. Here is my json data

{ 
"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" 
        } 
    ] 
}
}

and here is how I am trying to get it using 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);
        });

Help please. How can I achieve it? Above code doesnt work for me.

Here what the error is

object is undefined
length = object.length,                                   jquery-latest.js (line 630) 

I want to get the data inside the weather object. How can I get that?

Ok here is the image of output when I uncomment console.log enter link description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一枫情书 2025-01-01 10:55:50

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

$.each(w.data.weather, function(i, res) {        
    contents += "<section>" + res.tempMaxC + "</section>";
});

总而言之,您已经非常接近了。这是代码的工作小提琴,进行了细微的更改。

Your problem is your nested "each" loops. You are telling jQuery for each object in the response's data object, find each weather object inside of those and iterate over it. There is only one weather object and it's directly inside of data, so remove the outer loop and simplify with this single loop:

$.each(w.data.weather, function(i, res) {        
    contents += "<section>" + res.tempMaxC + "</section>";
});

All in all, you were pretty close. Here's a working fiddle of your code with the minor change.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文