Chris Dowdeswell
Chris Dowdeswell

Reputation: 868

Why is my jquery json each() loop returning undefined?

Here is my local Json which is validating in Jsonlint.

{
"messages": {
    "count": "0",
    "items": [
        {
            "MessageID": "1",
            "Starred": 0,
            "BodyPrev": "You wouldn't believe what has just happenedYou wouldn't believe what has ",
            "FromUserID": "1",
            "FromName": "Daisy Purdye",
            "FromUN": "daisypurdye",
            "Subject": "Yeayeah",
            "Body": "You wouldn't believe what has just happenedYou wouldn't believe what has just happenedYou wouldn't believe what has just happenedYou wouldn't believe what has just happenedYou wouldn't believe what has just happenedYou wouldn't believe what has just happened",
            "Ctime": "10/4/2012",
            "isRead": "1"
        },
        {
            "MessageID": "2",
            "Starred": 1,
            "BodyPrev": "Whatever",
            "FromUserID": "1",
            "FromName": "Daisy Purdye",
            "FromUN": "daisypurdye",
            "Subject": "Not true mate",
            "Body": "Whatever",
            "Ctime": "5/3/2012",
            "isRead": "1"
        }
    ]
}

}

and here is the jQuery to print out the messages...

    <script>
    $.getJSON("/json/messages.json",function(result){
        $.each(result, function(i, messages){
            console.log(messages.items.Subject)
        });
      });
   </script>

It is just returning undefined.

Upvotes: 1

Views: 2665

Answers (6)

subodh
subodh

Reputation: 6158

your object exist in this form

MainObj---> messages -----> items---->Subject

so if you need to print the subject then you have to access in the same manner as they exist

result.messages.items.Subject

Upvotes: 0

Carlos Martinez T
Carlos Martinez T

Reputation: 6528

There seems to be a problem with the logic. Try this:

<script>
        $.getJSON("/json/messages.json",function(result){
            $.each(result.items, function(i, item){
                console.log(item.Subject)
            });
          });
       </script>

Upvotes: 0

Guffa
Guffa

Reputation: 700810

The items property is an array, so you can't use items.Subject.

Loop through the items in the array, instead of looping through the properties in the root object:

$.getJSON("/json/messages.json",function(result){
  $.each(result.messages.items, function(i, item){
    console.log(item.Subject)
  });
});

Upvotes: 0

Anoop
Anoop

Reputation: 23208

items is an array. you should iterate through it to get all items.

 <script>
    $.getJSON("/json/messages.json",function(result){
        $.each(result, function(i, messages){
          $.each(messages.items, function(index, item){
                 console.log(item.Subject)
           });

        });
      });
   </script>

Upvotes: 0

jenson-button-event
jenson-button-event

Reputation: 18961

Items is itself an array so im guessing you need to at least access it like:

messages.items[0].Subject

Upvotes: 0

Denys S&#233;guret
Denys S&#233;guret

Reputation: 382464

$.each should receive an array and you pass it the root object, which isn't an array, as your array of messages is in result.messages.items.

To iterate over the messages, you should do

  $.getJSON("/json/messages.json",function(result){
    $.each(result.messages.items, function(i, message){
        console.log(message.Subject)
    });
  });

Upvotes: 5

Related Questions