user2969404
user2969404

Reputation: 27

How can I show this json data

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

Answers (3)

A l w a y s S u n n y
A l w a y s S u n n y

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

Oleksandr T.
Oleksandr T.

Reputation: 77512

Try this

for (var k in data.two) {
  console.log(data.two[k].a);
}

Example

Update:

$.each(data.two, function (i, value) {
  console.log(value.a);
});

Example

Upvotes: 3

satchcoder
satchcoder

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

Related Questions