4

My json data is this:

{
   "guide":0,
   "reunits":[
      {
         "residence":[
            {
               "name_re":"THE PORT",
               "id":"88aa355ca54853640929c25c33613528"
            }
         ]
      },
      {
         "residence":[
            {
               "name_re":"KECIK",
               "id":"2843543fa45857d92df3de222938e84a"
            }
         ]
      },
      {
         "residence":[
            {
               "name_re":"GREEN ANKA",
               "id":"fe585cc4b4dfff1325373728929e8af9"
            }
         ]
      }
   ]
}

How can done alert, value name_re or id in json data above by each in jquery?

My try:

$.each(data.reunits, function (index, value) {
    alert(value.residence.name_re); // this don't output.
})
7
  • 1
    try alert(value.residence[0].name_re); looks like array Commented Dec 23, 2011 at 20:29
  • I tried it, but i get just one data(THE PORT) no all three. Commented Dec 23, 2011 at 20:31
  • @KateWintz use a for...in loop Commented Dec 23, 2011 at 20:32
  • you still need the $.each for it to iterate through, I just fixed the statement with the bug Commented Dec 23, 2011 at 20:33
  • @Shiplu: Never use a for...in loop. Commented Dec 23, 2011 at 20:33

5 Answers 5

3

residence is an array ([]), which has only one element, an object that has a name_re attribute.

alert(value.residence[0].name_re);

jsFiddle Demo

Sign up to request clarification or add additional context in comments.

1 Comment

Works fine for me. See jsFiddle (edited it into my answer). Please check for differences between that and your code/JSON.
2

It's because they're within arrays. You need to access index 0 for each of them:

$.each(data.reunits, function(index, value) {
    alert(value.residence[0].name_re);
});

2 Comments

@KateWintz: That's strange. Can you make a jsFiddle and post it?
@minitech Revenge downvoting? I'd also be interested why the downvote.
1

residence is an array ,so you have to do the following js fiddle

    $(document).ready(function() {
        var data = {
            "guide": 0,
            "reunits": [
                {
                "residence": [
                    {
                    "name_re": "THE PORT",
                    "id": "88aa355ca54853640929c25c33613528"}
                ]},
            {
                "residence": [
                    {
                    "name_re": "KECIK",
                    "id": "2843543fa45857d92df3de222938e84a"}
                ]},
            {
                "residence": [
                    {
                    "name_re": "GREEN ANKA",
                    "id": "fe585cc4b4dfff1325373728929e8af9"}
                ]}
            ]
        };
        $.each(data.reunits, function() {
            $.each(this.residence, function() {
                alert(this.name_re);
                alert(this.id);
            });
        });
    });

1 Comment

@minitech i have formatted it,this inside the $.each ,will give you the current item,check the js fiddle link :)
0

value.residence is an array. It should be value.residence[0].name_re.

$.each(data.reunits, function (index, value) {
    alert(value.residence[0].name_re);
})

1 Comment

@KateWintz: Then something else is wrong, as it works for me. jsfiddle.net/dNBVb
0
var data = JSON;
var reunits = data.reunits;
for (var i = 0, len = reunits.length; i < len; i++) {
    alert(reunits[i].residence[0].name_re);
}

1 Comment

Hmm nice catch, fixed the problem.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.