2017-10-07 82 views
0

我有以下格式從映射服務返回的位置列表。從位置字符串創建JSON對象

[115.772933,-32.095437], [115.772933,-32.095437], 
    [115.772933,-32.095437], 

我需要插入到MongoDB的集合作爲一個有效的JSON對象這一點,但我不知道如何嵌入一個字符串轉換爲JavaScript中有效的JSON對象。我試過JSONparse,但我得到的錯誤。

var coordinates = "[115.772933,-32.095437], [115.772933,-32.095437], [115.772933,-32.095437]," 

     var polygons = { 
    "type" : "Polygon", 
    "coordinates" : [ 
     [ 
    coordinates 
     ] 
    ] 
} 

回答

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);