вставьте элементы JSON в массив - PullRequest
1 голос
/ 29 ноября 2011

Как перенести элементы, возвращенные из этого json-файла, в массив, называемый местами:

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 ]

2 голосов
/ 29 ноября 2011

Просто push данные в массив, например:

places.push(item.latitude+','+ item.longitude);
0 голосов
/ 29 ноября 2011

Попробуйте следующее:

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.');
  }
});
0 голосов
/ 29 ноября 2011
success: function(data, status) {
    $.each(data, function(i, item) {
        places.push( item.latitude+','+ item.longitude );
    });
},
...