简体   繁体   English

在JavaScript中访问JSON对象中的数据(已解析)

[英]Access data in a JSON object (parsed) in JavaScript

I need to get the data "messages" from this JSON object. 我需要从这个JSON对象获取数据“消息”。 How can I do it in JavaScript? 我怎么能在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": "the@email.com"
        }
    }
]
response[i].user.last_message.message.created_at

这是一个现场演示

response[i].user.last_message.message

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 JSFiddle工作示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM