1

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.

6 Answers 6

5

$.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)
    });
  });
Sign up to request clarification or add additional context in comments.

1 Comment

Excellent, thanks. This was one of those problems my tired mind could have looked at it all day long and still not seen the obvious :D this makes perfect sense. Ill accept the answer in a few mins when I'm allowed.
0

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

messages.items[0].Subject

Comments

0

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>

Comments

0

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)
  });
});

Comments

0

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>

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.