将json项目推入数组

时间:2011-11-29 10:44:31

标签: jquery jsonp

如何将从此json文件返回的项目推送到名为places:

的数组中
var places = [];

$.ajax({    
    url: "http://www.example.com/places.json",
    dataType: 'jsonp',
    timeout: 5000,
    success: function(data, status) {
        $.each(data, function(i, item) {
            places = item.latitude+','+ item.longitude;    
        });
    },
    error: function() {
        $("#status").html('There was an error loading the data.');
    }
});

3 个答案:

答案 0 :(得分:2)

简单push将数据放入数组中,如下所示:

places.push(item.latitude+','+ item.longitude);

答案 1 :(得分:0)

success: function(data, status) {
    $.each(data, function(i, item) {
        places.push( item.latitude+','+ item.longitude );
    });
},

答案 2 :(得分:0)

尝试以下方法:

var places = [];

$.ajax({    
  url: "http://www.example.com/places.json",
  dataType: 'jsonp',
  timeout: 5000,
  success: function(data, status) {
    $.each(data, function(i, item) {
        var place = item.latitude+','+ item.longitude;
        places.push(place);
    });
  },
  error: function() {
    $("#status").html('There was an error loading the data.');
  }
});