7

I need to get the data "messages" from this JSON object. How can I do it in JavaScript?

To access for example the lastname I just use:

response[i].user.lastname

But how can I access the messages?

[
 {
 "user": {
 "last_message": {
 "message": {
 "created_at": "2011-04-16T16:40:56Z",
 "updated_at": "2011-04-16T16:40:56Z",
 "to": null,
 "id": 10,
 "user_id": 28,
 "message": "This is a message"
 }
 },
 "nickname": "thenicky",
 "id": 28,
 "lastname": "white",
 "firstname": "Sean",
 "bio": "A short bio",
 "email": "[email protected]"
 }
 }
]
halfer
20.2k20 gold badges111 silver badges208 bronze badges
asked Apr 16, 2011 at 22:05

3 Answers 3

8
response[i].user.last_message.message.created_at

And here's a live demo.

answered Apr 16, 2011 at 22:07
Sign up to request clarification or add additional context in comments.

3 Comments

I get this error when I try it: Result of expression 'response[i].user.last_message' [undefined] is not an object.
@Jonathan Clark, works fine for me. Checkout the following live demo: jsfiddle.net/Dwxj2/1
Works fine now. It was a problem with cache. Thanks!
3

response[i].user.last_message.message

answered Apr 16, 2011 at 22:07

1 Comment

What a fantastic answer. This deserves more recognition!
2

Some examples:

alert(response[0].user.last_message.message.id); 
alert(response[0].user.nickname); 
alert(response[0].user.lstname); 

and working code:

JSFiddle Working Example

answered Apr 16, 2011 at 22:18

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.