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

时间:2011-04-16 22:05:34

标签: 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": "the@email.com"
        }
    }
]

3 个答案:

答案 0 :(得分:8)

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

这是一个live demo

答案 1 :(得分:3)

response[i].user.last_message.message

答案 2 :(得分:2)

一些例子:

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

和工作代码:

JSFiddle Working Example