将Geojson加载到OpenLayers 3 vectorlayer时出错:JSON.PARSE预期属性名称



我正在尝试将geojson字符串加载到vectorlayer中,但面对JSON.parse: expected property name or '}' at line 1 column 2 of the JSON data问题。

已经读取有关JSON.PARSE的SOM文本:预期的属性名称或JSON数据的第1列第1列,但无法解决我的问题。

这是我的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和您的错误消息,我会尝试一个答案。第2列是单个引号。JSON需要双引号来容纳字符串,请参阅此问题。

另外,从geojson规范中:

功能对象必须具有名称为"属性"的成员。属性成员的值是一个对象(任何JSON对象或JSON NULL值)。

您的功能没有属性。

随着这两个更改:

{"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显示

最新更新