【发布时间】:2011-10-30 18:44:35
【问题描述】:
我正在尝试将从 JSON 文件中检索到的一组对象添加到数组中。我试过使用push,但结果是一样的。数组的length 值仍为0。我究竟做错了什么? JSON 解析得很好,因为我可以在循环期间获取值。
<script type="text/javascript">
//my array
var myArray = new Array();
function performSearch(){
var url = "http://myjsonurl...";
var counter = 0;
$.getJSON(url, function(response){
$.each(response.data.people, function() {
//p is the the object to add to the array
var p = new person(this.name, this.age);
//tried using myArray.push instead of having a counter, but
//I get the same length of 0.
myArray[counter] = p;
counter++;
});
});
//always returns 0
alert(myArray.length);
}
...
</script>
【问题讨论】:
标签: javascript arrays json push