将 GeoJSON 加载到 OpenLayers 3 VectorLayer 时出错:JSON.parse 预期 属性 名称
Error when loading GeoJSON to OpenLayers 3 VectorLayer: JSON.parse expected property name
我正在尝试将 GeoJSON 字符串加载到 VectorLayer 中,但遇到 JSON.parse: expected property name or '}' at line 1 column 2 of the JSON data
问题。
已经阅读了一些类似 JSON.parse: expected property name or '}' at line 1 column 2 of the JSON data 的文章,但无法解决我的问题。
这是我的JSON。在 http://jsonviewer.stack.hu/.
测试
{'type': 'FeatureCollection','crs': {'type': 'name','properties': {'name': 'EPSG:4326'}},'features': [{'type': 'Feature','geometry':{'type':'MultiLineString','coordinates':[[[-43.1996056,-22.9109588],[-43.1993777,-22.9115575],[-43.1993539,-22.9116778],[-43.1993568,-22.9118156],[-43.199378,-22.9123812]]]}},{'type': 'Feature','geometry':{'type':'MultiLineString','coordinates':[[[-43.199378,-22.9123812],[-43.1994332,-22.9131767]]]}},{'type': 'Feature','geometry':{'type':'MultiLineString','coordinates':[[[-43.1994332,-22.9131767],[-43.1994563,-22.9141351],[-43.1994364,-22.9142456],[-43.199379,-22.9143303]]]}},{'type': 'Feature','geometry':{'type':'MultiLineString','coordinates':[[[-43.199379,-22.9143303],[-43.1985846,-22.9144791]]]}}]}
我的 OpenLayers 东西(featuresText
是上面的 GeoJSON):
var styleFunction = function(feature) {
return styles[feature.getGeometry().getType()];
};
var vectorSource = new ol.source.Vector({
features: ( new ol.format.GeoJSON() ).readFeatures( featuresText )
});
var vectorLayer = new ol.layer.Vector({
source: vectorSource,
style: styleFunction
});
错误在 ol-debug.js:45632
。 JSON 字符串似乎没问题。我想不通。
我也读过这个http://openlayers.org/en/master/examples/geojson.html。
查看您的geojson 和您的错误消息,我会尝试回答。第一行第二列是单引号。 JSON 需要双引号来保存字符串,see this question。
此外,来自 geojson specification:
A feature object must have a member with the name "properties". The value of the properties member is an object (any JSON object or a JSON null value).
您的功能没有属性。
有了这两个变化:
{"type": "FeatureCollection", "crs": {"type": "name","properties": {"name": "EPSG:4326"}},"features": [{"type": "Feature","properties": {}, "geometry":{"type":"MultiLineString","coordinates":[[[-43.1996056,-22.9109588],[-43.1993777,-22.9115575],[-43.1993539,-22.9116778],[-43.1993568,-22.9118156],[-43.199378,-22.9123812]]]}},{"type": "Feature","properties": {}, "geometry":{"type":"MultiLineString","coordinates":[[[-43.199378,-22.9123812],[-43.1994332,-22.9131767]]]}},{"type": "Feature","properties": {}, "geometry":{"type":"MultiLineString","coordinates":[[[-43.1994332,-22.9131767],[-43.1994563,-22.9141351],[-43.1994364,-22.9142456],[-43.199379,-22.9143303]]]}},{"type": "Feature","properties": {}, "geometry":{"type":"MultiLineString","coordinates":[[[-43.199379,-22.9143303],[-43.1985846,-22.9144791]]]}}]}
我在 geojson.io
成功显示了你的 geojson
我正在尝试将 GeoJSON 字符串加载到 VectorLayer 中,但遇到 JSON.parse: expected property name or '}' at line 1 column 2 of the JSON data
问题。
已经阅读了一些类似 JSON.parse: expected property name or '}' at line 1 column 2 of the JSON data 的文章,但无法解决我的问题。
这是我的JSON。在 http://jsonviewer.stack.hu/.
测试{'type': 'FeatureCollection','crs': {'type': 'name','properties': {'name': 'EPSG:4326'}},'features': [{'type': 'Feature','geometry':{'type':'MultiLineString','coordinates':[[[-43.1996056,-22.9109588],[-43.1993777,-22.9115575],[-43.1993539,-22.9116778],[-43.1993568,-22.9118156],[-43.199378,-22.9123812]]]}},{'type': 'Feature','geometry':{'type':'MultiLineString','coordinates':[[[-43.199378,-22.9123812],[-43.1994332,-22.9131767]]]}},{'type': 'Feature','geometry':{'type':'MultiLineString','coordinates':[[[-43.1994332,-22.9131767],[-43.1994563,-22.9141351],[-43.1994364,-22.9142456],[-43.199379,-22.9143303]]]}},{'type': 'Feature','geometry':{'type':'MultiLineString','coordinates':[[[-43.199379,-22.9143303],[-43.1985846,-22.9144791]]]}}]}
我的 OpenLayers 东西(featuresText
是上面的 GeoJSON):
var styleFunction = function(feature) {
return styles[feature.getGeometry().getType()];
};
var vectorSource = new ol.source.Vector({
features: ( new ol.format.GeoJSON() ).readFeatures( featuresText )
});
var vectorLayer = new ol.layer.Vector({
source: vectorSource,
style: styleFunction
});
错误在 ol-debug.js:45632
。 JSON 字符串似乎没问题。我想不通。
我也读过这个http://openlayers.org/en/master/examples/geojson.html。
查看您的geojson 和您的错误消息,我会尝试回答。第一行第二列是单引号。 JSON 需要双引号来保存字符串,see this question。
此外,来自 geojson specification:
A feature object must have a member with the name "properties". The value of the properties member is an object (any JSON object or a JSON null value).
您的功能没有属性。
有了这两个变化:
{"type": "FeatureCollection", "crs": {"type": "name","properties": {"name": "EPSG:4326"}},"features": [{"type": "Feature","properties": {}, "geometry":{"type":"MultiLineString","coordinates":[[[-43.1996056,-22.9109588],[-43.1993777,-22.9115575],[-43.1993539,-22.9116778],[-43.1993568,-22.9118156],[-43.199378,-22.9123812]]]}},{"type": "Feature","properties": {}, "geometry":{"type":"MultiLineString","coordinates":[[[-43.199378,-22.9123812],[-43.1994332,-22.9131767]]]}},{"type": "Feature","properties": {}, "geometry":{"type":"MultiLineString","coordinates":[[[-43.1994332,-22.9131767],[-43.1994563,-22.9141351],[-43.1994364,-22.9142456],[-43.199379,-22.9143303]]]}},{"type": "Feature","properties": {}, "geometry":{"type":"MultiLineString","coordinates":[[[-43.199379,-22.9143303],[-43.1985846,-22.9144791]]]}}]}
我在 geojson.io
成功显示了你的 geojson