0

Openweathermap APIを使用して単純な 1 ページの天気予報アプリを作成していますが、返されたネストされた 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)ます。

ネストされた weather オブジェクトと temp オブジェクトをループするにはどうすればよいですか? また、現在のコードで未定義のエラーが発生するのはなぜですか? コードと 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
4

1 に答える 1

1

各リストiから th 要素のみを求めています。から単一の要素を要求するのではなく、 をobj.list[i].weatherループする必要があります。obj.list[i].weatherしたがって、 に対してobj.list[0]は、 のみを要求しobj.list[0].weather[0]ます。の場合、存在しないobj.list[1]のみを要求obj.list[1].weather[1]します (そのweatherリストには index に単一のオブジェクトしかありません0)。

$.each(obj.list[i].weather[i], ...)ループしますが、ループしませobj.list[i].weather[i]obj.list[i].weatherobj.list[i].weather[j]のように、2 つの異なるループ変数を持つものが必要です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 );
                    });
                });
        }

    }

ここではobj.list[i].weather、内側の loping 変数を使用してループを追加しj、各weatherリストのアイテムを 1 つだけではなくすべて取得できるようにします。

于 2014-11-20T14:33:41.690 に答える