Javascript 如何访问JSON对象中的数组?

Javascript 如何访问JSON对象中的数组?,javascript,jquery,arrays,json,object,Javascript,Jquery,Arrays,Json,Object,我有以下JSON对象: [ { "comments": [ { "created_at": "2011-02-09T14:42:42-08:00", "thumb": "xxxxxxx", "level": 1,

我有以下JSON对象:

[
    {
        "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"
    }
]

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

返回的JSON实际上是一个数组本身,所以

dataJS[0].comments[0].created_at
将是
2011-02-09T14:42:42-08:00
,等等


dataJS
comments
都是数组,需要索引才能访问相应的元素。

返回的对象本身就是一个数组,因此要访问第一条注释(作为示例),您可以这样访问它:


dataJS[0]。注释[0]

必须使用
eval
函数解释JSON(经过明显的清理后,请参阅eval的安全注意事项)。你确定你的框架能为你做到这一点吗?

这是因为你的基本对象也是一个数组

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

我怀疑这样做会奏效

像这样做:-

console.log(dataJS);
console.log(dataJS[0].eee1);
console.log(dataJS[0].comments[0]);
var dataJS = [{"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"}];

var created_at = dataJS[0].comments[0].created_at;
var-dataJS=[{“comments”:[{“created_at:”2011-02-09T14:42-08:00“,”thumb:”xxxxxxx“,”level“:1,“id”:214,“user_id”:41,“parent_id”:213,“content:”xxxxxx

”,“full_name:”xx K“,”,{“created_at:”2011-02-09T14:41:23-08:00“,”thumb:”xxxxxxxxxxx“,”level“,”0,“id”:213,“user_id”:19,“parent_id”:null“,这是另一项测试,“全名”:“asd asd asd asd asd”}],“eee1”:“asdadsdas”,“eee2”:“bbbbb”}]; var created_at=dataJS[0]。注释[0]。created_at;
是的,正如其他人所说,JSON实际上是一个数组(单个对象的数组),因此需要引用索引


有趣的是(对我来说),您的结果字符串确实成功地验证为JSON。到目前为止,我一直认为要成为有效的JSON,它必须是一个对象(即,{})。

您必须在ajax请求中将数据类型称为“JSON”。让用户像下面这样做

 $.ajax({
            url: $('#frmAddCourse').attr('action'),
            type: 'POST',
            data: $('#frmAddCourse').serialize(),
            dataType: 'JSON',
            success: function (data){
                Materialize.toast(data['state'],2000);
            },
            error:function(){
                Materialize.toast(errorMessage,2000);
            }
        });

console.log(dataJS.comments[0]);?你检查过响应头的内容类型是否真的是application/json吗?@apreprentice你能用正确的答案对线程进行响应吗?是的,jQuery计算json.implicated by
$。ajax
和jQuery tagI gusted$。ajax可以是任何框架,就像大多数人通常使用$。ajax是一个非常好的工具基本的组合,所以不清楚。@Brian:大多数时候我同意,但是没有它的解析只是浪费资源。-而且服务器是一个可信的源,你的JS代码来自那里。@Brian:json2库使用
eval
。不,它不是根据规范构建的,所以不,它不是“不正确的”,但是你想使用什么语法访问它?好的,这就是我需要知道的。谢谢!一旦stackoverflow让我。。。