我有以下格式从地图服务返回的位置列表。
[115.772933,-32.095437], [115.772933,-32.095437],
[115.772933,-32.095437],
我需要将它作为有效的JSON对象插入到mongoDB集合中,但我不确定如何在javascript中将字符串嵌入到有效的JSON对象中。我试过JSONparse但是我收到了错误。
var coordinates = "[115.772933,-32.095437], [115.772933,-32.095437], [115.772933,-32.095437],"
var polygons = {
"type" : "Polygon",
"coordinates" : [
[
coordinates
]
]
}
答案 0 :(得分:2)
"use strict";
let coordinates = [[115.772933,-32.095437], [115.772933,-32.095437], [115.772933,-32.095437]];
let polygons = {
'type': 'Polygon',
'coordinates': coordinates
};
let jsonData = JSON.stringify(polygons);
console.log(jsonData);