如何删除 GEOjson 中多余的 " (Javascript)
How to delete an extra " in GEOjson (Javascript)
我试图操纵一个对象 GEOjson 以将其显示在我的地图 OpenLayers 中。但是我的 GEOjson 无效。
首先,在我的 API 中,我请求我的数据库恢复几何图形,然后我通过 GEOjson.parse 解析它:GeoJSON.parse(data, {GeoJSON : 'geometry'});
数据如下所示:
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": "{\"type\":\"MultiPolygon\",\"coordinates\":[[[[0.699526704561417,49.5855766259652],[0.699132813373672,49.5855472259388],[0.698829663954256,49.5852457878428],[0.698308423811369,49.5855523688003],[0.699127661396565,49.5862481964213],[0.699752271011022,49.5859030239836],[0.699526704561417,49.5855766259652]]]]}",
"properties": {
"libgeo": "Bois-Himont",
"nature": "Parcelle bâtie"
}
},
{
"type": "Feature",
"geometry": "{\"type\":\"MultiPolygon\",\"coordinates\":[[[[0.696220319484454,49.581274516207],[0.696272071456392,49.5820438187077],[0.697147417334422,49.5815673912038],[0.697102005023975,49.5814546891317],[0.697047441685103,49.5812624281067],[0.6969844037675,49.5812621313821],[0.696220319484454,49.581274516207]]]]}",
"properties": {
"libgeo": "Bois-Himont",
"nature": "Parcelle bâtie"
}
}, etc...
然后在我的 script.js(该文件的目的是显示地图和 GEOjson)中,由于 JSON.parse,我解析了数据,但是线几何无效,因为有一个 extra " 并且类型和坐标被包围在 ".
如何删除类型和坐标的多余“和删除”?
几何图形是一个字符串,但它应该是一个对象。第一次解析后,您需要循环遍历特征并将每个几何字符串解析为一个对象。
var myGeoJSON = JSON.parse(myText);
myGeoJSON.features.forEach( function(feature) { feature.geometry = JSON.parse(feature.geometry) });
我试图操纵一个对象 GEOjson 以将其显示在我的地图 OpenLayers 中。但是我的 GEOjson 无效。
首先,在我的 API 中,我请求我的数据库恢复几何图形,然后我通过 GEOjson.parse 解析它:GeoJSON.parse(data, {GeoJSON : 'geometry'});
数据如下所示:
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": "{\"type\":\"MultiPolygon\",\"coordinates\":[[[[0.699526704561417,49.5855766259652],[0.699132813373672,49.5855472259388],[0.698829663954256,49.5852457878428],[0.698308423811369,49.5855523688003],[0.699127661396565,49.5862481964213],[0.699752271011022,49.5859030239836],[0.699526704561417,49.5855766259652]]]]}",
"properties": {
"libgeo": "Bois-Himont",
"nature": "Parcelle bâtie"
}
},
{
"type": "Feature",
"geometry": "{\"type\":\"MultiPolygon\",\"coordinates\":[[[[0.696220319484454,49.581274516207],[0.696272071456392,49.5820438187077],[0.697147417334422,49.5815673912038],[0.697102005023975,49.5814546891317],[0.697047441685103,49.5812624281067],[0.6969844037675,49.5812621313821],[0.696220319484454,49.581274516207]]]]}",
"properties": {
"libgeo": "Bois-Himont",
"nature": "Parcelle bâtie"
}
}, etc...
然后在我的 script.js(该文件的目的是显示地图和 GEOjson)中,由于 JSON.parse,我解析了数据,但是线几何无效,因为有一个 extra " 并且类型和坐标被包围在 ".
如何删除类型和坐标的多余“和删除”?
几何图形是一个字符串,但它应该是一个对象。第一次解析后,您需要循环遍历特征并将每个几何字符串解析为一个对象。
var myGeoJSON = JSON.parse(myText);
myGeoJSON.features.forEach( function(feature) { feature.geometry = JSON.parse(feature.geometry) });