【问题标题】:JSON for OpenWeatherMap API用于 OpenWeatherMap API 的 JSON
【发布时间】:2015-01-18 10:37:20
【问题描述】:

我正在使用Openweathermap API 编写一个简单的单页天气应用程序,但在循环返回的嵌套 JSON 对象时遇到问题。

这是 JSON 的示例:

"cod":"200",
"message":0.1986,
"city":{
    "id":"4781530",
    "name":"Reston",
    "coord":{
        "lon":-77.3545,
        "lat":38.9597
    },
    "country":"United States of America",
    "population":0
},
"cnt":2,
"list":[
    {
        "dt":1416499200,
        "temp":{
            "day":45.3,
            "min":33.48,
            "max":52,
            "night":34.47,
            "eve":50.59,
            "morn":33.48
        },
        "pressure":1015.56,
        "humidity":58,
        "weather":[
            {
                "id":801,
                "main":"Clouds",
                "description":"few clouds",
                "icon":"02d"
            }
        ],
        "speed":9.07,
        "deg":212,
        "clouds":24
    },
    {
        "dt":1416585600,
        "temp":{
            "day":33.04,
            "min":20.55,
            "max":38.86,
            "night":22.87,
            "eve":38.03,
            "morn":24.42
        },
        "pressure":1027.28,
        "humidity":50,
        "weather":[
            {
                "id":800,
                "main":"Clear",
                "description":"sky is clear",
                "icon":"01d"
            }
        ],
        "speed":7.99,
        "deg":301,
        "clouds":0
    }
]

}

我能够成功循环遍历 obj.list 中的第一组值,但是当它到达 $.each(obj.list[i].weather[i], function(key,value) 循环时出现未定义的错误。

我将如何遍历嵌套的天气和临时对象,为什么我当前的代码会给我一个未定义的错误?我已经包含了代码和 Firebug 控制台日志。非常感谢任何帮助。

jQuery 循环函数

$.each(obj.list, function(i) {
    $.each(obj.list[i], function(key,value) {

        if(typeof value == "object"){
            console.log(key + " is an Object!!");

            if(key=="weather"){
                            console.log("What is i? " + i );
                            console.log(obj.list[i].weather[i]);

                            $.each(obj.list[i].weather[i], function(key,value) {
                                    console.log("weather object " + key + ": " + value );
                            });
            }

        }
        else{
            console.log("outer object " + key + ": " + value );
        }
    });
});

Firebug 控制台日志:

outer object dt: 1416499200
scripts.js (line 72)
temp is an Object!!
scripts.js (line 49)
outer object pressure: 1015.56
scripts.js (line 72)
outer object humidity: 58
scripts.js (line 72)
weather is an Object!!
scripts.js (line 49)
What is i? 0
scripts.js (line 59)
Object { id=801, main="Clouds", description="few clouds", more...}
scripts.js (line 60)
weather object id: 801
scripts.js (line 66)
weather object main: Clouds
scripts.js (line 66)
weather object description: few clouds
scripts.js (line 66)
weather object icon: 02d
scripts.js (line 66)
outer object speed: 9.07
scripts.js (line 72)
outer object deg: 212
scripts.js (line 72)
outer object clouds: 24
scripts.js (line 72)
***********
scripts.js (line 103)
outer object dt: 1416585600
scripts.js (line 72)
temp is an Object!!
scripts.js (line 49)
outer object pressure: 1027.28
scripts.js (line 72)
outer object humidity: 50
scripts.js (line 72)
weather is an Object!!
scripts.js (line 49)
What is i? 1
scripts.js (line 59)
undefined

【问题讨论】:

    标签: jquery json weather-api openweathermap


    【解决方案1】:

    您只要求每个 obj.list[i].weather 列表中的 ith 元素。您应该循环遍历obj.list[i].weather,而不是从中请求单个元素。因此,对于obj.list[0],您只需要obj.list[0].weather[0]。对于obj.list[1],您只要求obj.list[1].weather[1],它不存在(weather 列表只有一个对象,位于索引0)。

    您的$.each(obj.list[i].weather[i], ...) 循环遍历obj.list[i].weather[i],但您不会循环遍历obj.list[i].weather。你需要两个不同的循环变量,比如obj.list[i].weather[j],而不是obj.list[i].weather[i]

        if(typeof value == "object"){
            console.log(key + " is an Object!!");
    
            if(key=="weather"){
                    console.log("What is i? " + i );
    
                    $.each(obj.list[i].weather, function(j) {
                        console.log(obj.list[i].weather[j]);
                        $.each(obj.list[i].weather[j], function(key,value) {
                            console.log("weather object " + key + ": " + value );
                        });
                    });
            }
    
        }
    

    在这里,我们使用内部循环变量jobj.list[i].weather 上添加一个循环,以便我们获得每个weather 列表中的所有项目,而不是只有一个..

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-08
      • 1970-01-01
      • 1970-01-01
      • 2018-04-10
      • 2020-03-04
      • 1970-01-01
      • 1970-01-01
      • 2021-06-24
      相关资源
      最近更新 更多