在我的JSON對象posts.php
我有兩個數組,posts
和comments
。下面是一個例子(posts
只是[...]在這個例子中,但comments
有2個例評論:循環JSON對象返回undefined某些元素
posts.php
{
"posts": [{
...
}],
"comments": [{
"id": "1",
"submitter": "",
"time": "1433601171",
"comment": "example comment",
"score": "0",
"postid": "1"
}, {
"id": "2",
"submitter": "",
"time": "1433601211",
"comment": "another comment",
"score": "0",
"postid": "1"
}]
}
的Javascript:
$.getJSON('posts.php',function(data){
data.comments.forEach(function(comment){
var id = comment.id;
var submitter = comment.submitter;
var time = comment.time;
var comment = comment.comment;
var score = comment.score;
var postid = comment.postid;
對於某些原因,當我alert(score)
和alert(postid)
他們是undefined
,但所有其他變量包含正確的信息。爲什麼?