Reputation: 27
In $.ajax I Get this json data. How can show All 'a'?
{
"one": "tt",
"two": {
"i1": {
"id": "1",
"a": "ff"
},
"i2": {
"id": 2,
"a": "gg"
}
},
"three": "kk"
}
Upvotes: 1
Views: 68
Reputation: 38552
Try this way using each
{
"one": "tt",
"two": {
"i1": {
"id": "1",
"a": "ff"
},
"i2": {
"id": 2,
"a": "gg"
}
},
"three": "kk"
}
$.each(data.two, function(i, item) {
alert(item.a);
});
Upvotes: 1
Reputation: 77512
Try this
for (var k in data.two) {
console.log(data.two[k].a);
}
Update:
$.each(data.two, function (i, value) {
console.log(value.a);
});
Upvotes: 3
Reputation: 797
I think this little recursive function can help to print all the 'a' properties of the json you received.
function show(obj)
{
var atts = Object.keys(obj);
atts.forEach(function (element, index, array)
{
if(element=='a') console.log(obj[element]);
show(obj[element]); // recursiveness
});
}
Upvotes: 1