【问题标题】:Error when loading GeoJSON to OpenLayers 3 VectorLayer: JSON.parse expected property name将 GeoJSON 加载到 OpenLayers 3 VectorLayer 时出错:JSON.parse 预期属性名称
【发布时间】:2016-12-15 18:11:23
【问题描述】:

我正在尝试将 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

【问题讨论】:

  • 为什么在这里而不是在 GIS 中?我认为这是一个 JSON 格式问题。

标签: javascript json openlayers-3 geojson


【解决方案1】:

查看您的 geojson 和错误消息,我会尝试回答。第一行第 2 列是单引号。 JSON 需要双引号来保存字符串,see this question

另外,来自geojson specification

要素对象必须有一个名为“properties”的成员。 properties 成员的值是一个对象(任何 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

【讨论】:

  • 太棒了。我发现有人说正好相反。 JSON 必须是单引号。我认为问题可能只是properties 成员。现在将尝试并发布结果。
  • 你是对的。现在一切正常(好吧,我在样式方面遇到了一些问题,但这是 GIS S.E. 的问题)。谢谢。顺便说一句,geojson.io 的 +1。
  • @MagnoC 在字符串上引用JSON specification„字符串是零个或多个Unicode 字符的序列,用双引号括起来,[…]“。跨度>
猜你喜欢
  • 1970-01-01
  • 2013-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多