Go through list of objects in JSON

0

I have this JSON

{
    "cod": "200",
    "message": 0.0025,
    "cnt": 40,
    "list": [
        {
        "dt": 1503586800,
            "main": {
                "temp": 307.92,
                "temp_min": 307.92,
                "temp_max": 308.292,
                "pressure": 956.05,
                "sea_level": 1026.51,
                "grnd_level": 956.05,
                "humidity": 19,
                "temp_kf": -0.37
            },
            "weather": [
                {
                    "id": 800,
                    "main": "Clear",
                    "description": "clear sky",
                    "icon": "01d"
                }
            ],
            "clouds": {
                "all": 0
            },
            "wind": {
                "speed": 6.57,
                "deg": 232.003
            },
            "sys": {
                "pod": "d"
            },
            "dt_txt": "2017-08-24 15:00:00"
        },
        {
            "dt": 1503597600,
            "main": {
                "temp": 305.14,
                "temp_min": 305.14,
                "temp_max": 305.416,
                "pressure": 956.03,
                "sea_level": 1026.69,
                "grnd_level": 956.03,
                "humidity": 17,
                "temp_kf": -0.28
            },
            "weather": [
                {
                    "id": 800,
                    "main": "Clear",
                    "description": "clear sky",
                    "icon": "01d"
                }
            ],
            "clouds": {
                "all": 0
            },
            "wind": {
                "speed": 5.57,
                "deg": 246
            },
            "sys": {
                "pod": "d"
            },
            "dt_txt": "2017-08-24 18:00:00"
        }
    ],
    "city": {
        "id": 3117735,
        "name": "Madrid",
        "coord": {
            "lat": 40.4165,
            "lon": -3.7026
        },
        "country": "ES"
    }
}

I have managed to show the city (city.name), and the coordinates (city.coord). But I would like to show the list.weather.main attribute. Which I think is within a list of objects. I do not know how to walk it. This is what I have:

$.each( data.city, function( key, val ) {
    if (key=="name"){
        items.push("Ciudad: "+val);
    }
});

$.each( data.city, function( key, val ) {
    if (key=="coord"){
        items.push(" Coordenadas: "+val.lat);
    }
});

And to try to show the list of objects, I do the following:

$.each( data.list, function( key, val ) {
    if (key=="weather"){
        items.push(" Coordenadas: "+val.main);
    }
});

But it does not work, any help?

    
asked by Alberto Buongiovanni Merida 24.08.2017 в 16:05
source

2 answers

1

First you have to access the property "list" by means of the notation object.property to go through it with the loop $.each

data.list
// Devuelve un arreglo que contiene objetos [{ .. }, { .. }, { .. }]

You can access the "weather" property of each item that is in the list using the period, like this:

value.weather
// Devuelve un arreglo que contiene un solo objeto [{ .. }]

Since weather stores an array, you have to use the index 0 to access it and again the dot notation.

value.weather[0].main
// Devuelve la cadena "Clear"

Everything together would look like this:

$.each( data.list, function (i, value) {
    items.push( value.weather[0].main );
});
    
answered by 24.08.2017 в 16:42
0

What happens is that list has no properties, but has indexes . First you have to go through the positions before entering their properties, for example:

$.each(data.list, function( oKey, oVal ) { //outter Key, outter Val
    $.each(data.list[oKey], function( key, val){ 
        if (key=="weather"){
            items.push(" Coordenadas: "+val.main);
        }
    });
});

However, within weather you also have an array with indexes, so you must also access each position (which is only one). If the weather fix will remain that way, with only one position, it should not be difficult:

$.each(data.list, function( oKey, oVal ) { //outter Key, outter Val
    $.each(data.list[oKey], function( key, val ){ 
        if (key=="weather"){
            items.push(" Coordenadas: " + val[0].main);
        }
    });
});

What would result in:

items = [" Coordenadas: Clear"," Coordenadas: Clear", ...]

Otherwise, you would need an additional iteration.

    
answered by 24.08.2017 в 16:38