Uncaught SyntaxError:意外令牌,JSON位置10

时间:2019-07-03 22:56:27

标签: javascript jquery

我收到一个损坏的json:

Array(1), "40.7197406, 8.563512299999957", "40.7272074, 8.575266499999998", Array(1), Array(1), Array(1), Array(1), Array(1), Array(1), Array(1), Array(1), Array(1), Array(1), Array(1)]
0: ["-22.91401497538739,-68.19866465000001"]
1: ["-25.857842171488155,-54.4140132"]

我尝试了JSON.parse(data[i].coordinates[i]),但是遇到了这个错误,json看起来好像有一些字符串,但是我不确定如何解决并使其正确

1 个答案:

答案 0 :(得分:1)

那不是JSON,所以不要尝试使用JSON.parse。只需用逗号将其分开,然后调用parseFloat()

var data = [{
  coordinates: ["40.7197406, 8.563512299999957", "40.7272074, 8.575266499999998"]
}];

var coords = data[0].coordinates.map(s => s.split(",").map(n => parseFloat(n.trim())));
console.log(coords);