简体   繁体   中英

Access data in a JSON object (parsed) in JavaScript

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": "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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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