在 JavaScript 中访问 JSON 对象(已解析)中的数据
我需要从这个 JSON 对象获取数据“消息”。我怎样才能在 JavaScript 中做到这一点?
例如,要访问我只需使用的姓氏:
response[i].user.lastname
但是如何访问消息?
[
{
"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]"
}
}
]
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]"
}
}
]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是现场演示。
And here's a live demo.
响应[i].user.last_message.message
response[i].user.last_message.message
一些示例:
和工作代码:
JSFiddle 工作示例
Some examples:
and working code:
JSFiddle Working Example