我从servlet中检索以下数据,我想将这些数据["-1.2396853,52.7680791","-1.2396572,52.7682574","-1.2396083,52.7684203"]
存储在下面的坐标中。
var coords = [[-1.2396853, 52.7680791],[-1.2396572, 52.7682574],[-1.2396083, 52.7684203]];
$.ajax({
type: "GET",
url: "servlet",
success: function(data) {
var coordinates = JSON.parse(data).toString();
var coords = [];
var coords = coordinates.split(",");
}
});
我有什么建议可以做到这一点吗?
答案 0 :(得分:1)
所以给出:
var o = ["-1.2396853,52.7680791","-1.2396572,52.7682574","-1.2396083,52.7684203"];
您可以将其转换为您的结果:
var result = o.map(function(i) {
var cells = i.split(",");
return [+cells[0],+cells[1]];
});
注意:您可能希望包含一些错误检查,以检查拆分是否为您提供了一对值。
答案 1 :(得分:0)
您的成功功能应该是
var coordinates = JSON.parse(data);
var coords = [];
for (var i=0, c; c = coordinates[i]; i++) {
coords.push(c.split(","));
}