如何访问 JSON 对象中的数组? [英] How to access an array in a JSON object?

查看:35
本文介绍了如何访问 JSON 对象中的数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 JSON 对象:

<预><代码>[{评论":[{created_at":2011-02-09T14:42:42-08:00",拇指":xxxxxxx",级别":1,id":214,用户 ID":41,parent_id":213,内容":<p>xxxxxx</p>",full_name":xx K"},{created_at":2011-02-09T14:41:23-08:00",拇指":xxxxxxxxxxxxx",级别":0,id":213,用户 ID":19,parent_id":空,内容":<p>这是另一个测试</p>",full_name":asd asd asd asd asd"}],eee1":asdadsdas",eee2":bbbbb"}]

这来自 $.ajax 请求,我成功了....

成功:函数(dataJS){控制台日志(数据JS);console.log(dataJS[eee1]);console.log(dataJS.comments);}

问题是我无法访问 JSON 对象中的项目,即使 dataJS 确实在控制台中正确显示.想法?

解决方案

那是因为你的基础对象也是一个数组.

console.log(dataJS[0].comments[0]);

我怀疑这行得通

I have the following JSON object:

[
    {
        "comments": [
            {
                "created_at": "2011-02-09T14:42:42-08:00",
                "thumb": "xxxxxxx",
                "level": 1,
                "id": 214,
                "user_id": 41,
                "parent_id": 213,
                "content": "<p>xxxxxx</p>",
                "full_name": "xx K"
            },
            {
                "created_at": "2011-02-09T14:41:23-08:00",
                "thumb": "xxxxxxxxxxxxx",
                "level": 0,
                "id": 213,
                "user_id": 19,
                "parent_id": null,
                "content": "<p>this is another test</p>",
                "full_name": "asd asd asd asd asd"
            }
        ],
        "eee1": "asdadsdas",
        "eee2": "bbbbb"
    }
]

This is coming from a $.ajax request, in success I have....

success: function (dataJS) {
    console.log(dataJS);
    console.log(dataJS[eee1]);
    console.log(dataJS.comments);
}

Problem is I can't get access to the items in the JSON object, even though dataJS does show correctly in the console. Ideas?

解决方案

That's because your base object is an array as well.

console.log(dataJS[0].comments[0]);

I suspect that would work

这篇关于如何访问 JSON 对象中的数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆