Magno C
Magno C

Reputation: 2196

Error when loading GeoJSON to OpenLayers 3 VectorLayer: JSON.parse expected property name

I'm trying to load a GeoJSON string into a VectorLayer but facing a JSON.parse: expected property name or '}' at line 1 column 2 of the JSON data problem.

Already read som texts about like JSON.parse: expected property name or '}' at line 1 column 2 of the JSON data but can't solve my problem.

This is my JSON. Tested at 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]]]}}]}

My OpenLayers stuff (featuresText is the GeoJSON above):

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

Error is at ol-debug.js:45632. The JSON string seems to be ok. I can't figure out.

I've read this http://openlayers.org/en/master/examples/geojson.html too.

Upvotes: 0

Views: 938

Answers (1)

Andrew Reid
Andrew Reid

Reputation: 38151

Looking at your geojson and your error message, I'll try an answer. Line one column 2 is a single quotation. JSON requires double quotes to hold strings, see this question.

Also, from the 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).

Your features have no properties.

With those two changes:

{"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]]]}}]}

I had your geojson display successfully at geojson.io

Upvotes: 2

Related Questions