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

AnA*_*ice 16 javascript arrays jquery json object

您好我有以下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"
 }
]
Run Code Online (Sandbox Code Playgroud)

这是来自一个$.ajax要求,我有成功....

    success: function (dataJS) {
        console.log(dataJS);
        console.log(dataJS[eee1]);
        console.log(dataJS.comments);
    }
Run Code Online (Sandbox Code Playgroud)

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

谢谢

Dar*_*o Z 13

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

console.log(dataJS[0].comments[0]);
Run Code Online (Sandbox Code Playgroud)

我怀疑这会奏效


jon*_*ohn 5

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

dataJS[0].comments[0].created_at
Run Code Online (Sandbox Code Playgroud)

2011-02-09T14:42:42-08:00等等......

这两个dataJScomments是数组,并且需要索引来访问相应的元素.