Gus Gus - 6 months ago 15
Ajax Question

Jquery read value of json nested object

I'm trying to read the value of a json nested object.

I have tried in these ways but without success.

data

{ "foo": 1,
"bar": 10,
"rows": [ {"id":1,"name":"Luke" },
{"id":2,"name":"Sky" },
{"id":3,"name":"Walker"} ]
}


Ajax

$.ajax({
data: params,
dataType: 'json',
success: function(data) {

console.log(data.rows) // data of rows object - OK !
console.log(data["rows"].id); // undefined
console.log(data.rows.id); // undefined
}
});


How could I do? Thank you

Answer

Rows is an array.

Change

console.log(data["rows"].id);

to

console.log(data["rows"][0].id);

You can also iterate on it to get all the values

for (var i = 0; i < data["rows"].length; i++) {
    console.log(data["rows"][i].id); 
}
Comments