一方面,您的变量包含一个具有一个元素的数组,该元素是一个对象。
所以为了访问内容,你必须comments[ INDEX ][ PROPERTYNAME ]
像这样使用:
var comments=[{"comment": "re", "author": "Anonym", "likes": 0, "key": "ahFzfmVhc3ljb21tZW50LWhyZHIQCxIHQ29tbWVudBj46qcJDA", "date": 1363460164.0, "approved": true}]
for(i=0;i<1;i++){
document.write(comments[i]['author'] + "<br>" + comments[i]['comment'] ) ;
}
一般来说,我会document.write()
用其他东西代替,它利用innerHTML
. 这可能看起来像这样:
<div id="commentBox"></div>
<script>
var comments=[{"comment": "re", "author": "Anonym", "likes": 0, "key": "ahFzfmVhc3ljb21tZW50LWhyZHIQCxIHQ29tbWVudBj46qcJDA", "date": 1363460164.0, "approved": true}],
commentBox = document.getElementById( 'commentBox' );
for(i=0;i<1;i++){
commentBox.innerHTML += comments[i]['author'] + "<br>" + comments[i]['comment'];
}
</script>