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
Jonathan Clark
20.6k29 gold badges116 silver badges178 bronze badges
3 Answers 3
response[i].user.last_message.message.created_at
And here's a live demo.
answered Apr 16, 2011 at 22:07
Darin Dimitrov
1.0m277 gold badges3.3k silver badges3k bronze badges
Sign up to request clarification or add additional context in comments.
3 Comments
Jonathan Clark
I get this error when I try it: Result of expression 'response[i].user.last_message' [undefined] is not an object.
Darin Dimitrov
@Jonathan Clark, works fine for me. Checkout the following live demo: jsfiddle.net/Dwxj2/1
Jonathan Clark
Works fine now. It was a problem with cache. Thanks!
response[i].user.last_message.message
answered Apr 16, 2011 at 22:07
Zirak
40k13 gold badges85 silver badges92 bronze badges
1 Comment
Benjamin Gruenbaum
What a fantastic answer. This deserves more recognition!
Some examples:
alert(response[0].user.last_message.message.id);
alert(response[0].user.nickname);
alert(response[0].user.lstname);
and working code:
answered Apr 16, 2011 at 22:18
Kerem Baydoğan
10.8k1 gold badge45 silver badges53 bronze badges
Comments
lang-js