我有以下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"
}
]这是来自$.ajax的请求,在成功中我有....
success: function (dataJS) {
console.log(dataJS);
console.log(dataJS[eee1]);
console.log(dataJS.comments);
}问题是我无法访问JSON对象中的项,即使dataJS确实在控制台中正确显示。想法?
发布于 2011-03-03 05:21:05
这是因为你的基对象也是一个数组。
console.log(dataJS[0].comments[0]);我怀疑那会行得通
发布于 2011-03-03 05:20:14
返回的JSON实际上是一个数组本身,所以...
dataJS[0].comments[0].created_at将是2011-02-09T14:42:42-08:00,等等。
dataJS和comments都是数组,需要索引才能访问相应的元素。
发布于 2011-03-03 05:20:46
返回的对象本身是一个数组,所以要获得第一个注释(作为示例),您可以这样访问它:
dataJS[0].comments[0]
https://stackoverflow.com/questions/5173603
复制相似问题