Javascript 如何访问 JSON 对象中的数组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5173603/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to access an array in a JSON object?
提问by AnApprentice
Hello I have the following 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"
}
]
This is coming from a $.ajax
request, in success I have....
这是来自一个$.ajax
请求,我成功了......
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?
问题是我无法访问 JSON 对象中的项目,即使 dataJS 确实在控制台中正确显示。想法?
Thanks
谢谢
采纳答案by Darko Z
That's because your base object is an array as well.
那是因为您的基础对象也是一个数组。
console.log(dataJS[0].comments[0]);
I suspect that would work
我怀疑这会起作用
回答by jondavidjohn
the JSON you have coming back is actually an array itself, so...
你回来的 JSON 实际上是一个数组本身,所以......
dataJS[0].comments[0].created_at
will be 2011-02-09T14:42:42-08:00
, etc...
将是2011-02-09T14:42:42-08:00
,等等...
Both dataJS
and comments
are arrays, and need indexes to access the appropriate elements.
这两个dataJS
和comments
是数组,并且需要索引来访问相应的元素。
回答by Brian Driscoll
The object being returned is itself an array, so to get to the first comment (as an example), this is how you would access it:
返回的对象本身就是一个数组,因此要获得第一个注释(作为示例),您可以通过以下方式访问它:
dataJS[0].comments[0]
dataJS[0].comments[0]
回答by Detect
console.log(dataJS);
console.log(dataJS[0].eee1);
console.log(dataJS[0].comments[0]);
回答by limc
Do something like this:-
做这样的事情:-
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;
回答by Ryan Miller
Yes, as others have stated, the JSON is actually an Array (of a single Object). So you will need to reference an index.
是的,正如其他人所说,JSON 实际上是一个数组(单个对象的)。所以你需要引用一个索引。
Interestingly enough (to me), your result string does validate successfully as JSON. I assumed until now, that to be valid JSON, it had to be an Object (ie, {}).
有趣的是(对我而言),您的结果字符串确实成功验证为 JSON。到目前为止,我一直认为,要成为有效的 JSON,它必须是一个对象(即 {})。
回答by yasankarj
You have to mention the dataType in the ajax request as 'JSON'. Make user you did that like below.
您必须将 ajax 请求中的 dataType 称为“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);
}
});
回答by vbence
JSON must be interpreted with eval
function (after the obvious sanitization, see security considerations of eval). Are you sure your framework does that for you?
JSON 必须用eval
函数来解释(在明显的清理之后,请参阅 eval 的安全注意事项)。您确定您的框架可以为您做到这一点吗?