无法解析Json文件

时间:2016-02-25 19:34:34

标签: javascript jquery json

我想加载上传到服务器上的GeoJson文件

var promise = ('https://api.myjson.com/bins/31e3j');
that.map.data.loadGeoJson(promise);

此条件正常

但我想在本地加载这个GeoJson文件 所以我已经将Json代码分配给变量的服务器链接,在该变量上我既没有收到任何错误也没有得到O / P

var promise = jQuery.parseJSON ('{ "type": "FeatureCollection","crs":{"type": "name","properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84"}},"features": [{"type": "Feature", "properties": {"id": 1},"geometry": {"type": "Polygon", "coordinates": [ [  [  -83.52936044652942, 40.30230752849768], [ -83.52924865349425,  40.30230753872012], [ -83.52924666169983, 40.3021800251207 ], [ -83.52935848418728, 40.302181900418084 ], [ -83.52936044652942, 40.30230752849768]]]}}, ]}');
that.map.data.loadGeoJson(promise);

3 个答案:

答案 0 :(得分:5)

如有疑问,请通过linter / formatter运行:

http://jsonlint.com/

你在JSON中有一个错误,最后是一个逗号:

]]]}}, ]}');
     ^-------TROUBLE MAKER!

或者这个很酷!

http://pro.jsonlint.com/

  

我既没有收到任何错误

也许周围的代码吞噬了错误。如果您使用var promise = jQuery.parseJSON('DODGY_JSON_HERE')代码并在控制台中运行,则会看到错误:

Uncaught SyntaxError: Unexpected token ](…)
   e.extend.parseJSON              @jquery.min.js:2
   (anonymous function)            @VM270:2
   InjectedScript._evaluateOn      @VM268:875
   InjectedScript._evaluateAndWrap @VM268:808
   InjectedScript.evaluate         @VM268:664

不像短信一样方便,但至少你看到 错误。

答案 1 :(得分:2)

无效的JSON显然不可解析:

...snip...[ -83.52936044652942, 40.30230752849768]]]}}, ]}');
                                                       ^----

答案 2 :(得分:1)

因为这不是正确的JSON。最后还有其他逗号。

{" type":" FeatureCollection"," crs":{" type":" name", "属性":{"名称":" urn:ogc:def:crs:OGC:1.3:CRS84"}},"功能" :[{" type":" Feature"," properties":{" id":1}," geometry&#34 ;:{"输入":"多边形","坐标":[[[-83.52936044652942,40.30230752849768],[ - 83.52924865349425,40.30230753872012],[ - 83.52924666169983, 40.3021800251207],[ - 83.52935848418728,40.302181900418084],[ - 83.52936044652942,40.30230752849768]]]}}]}

这是正确的JSON:

{
    "type": "FeatureCollection",
    "crs": {
        "type": "name",
        "properties": {
            "name": "urn:ogc:def:crs:OGC:1.3:CRS84"
        }
    },
    "features": [
        {
            "type": "Feature",
            "properties": {
                "id": 1
            },
            "geometry": {
                "type": "Polygon",
                "coordinates": [
                    [
                        [
                            -83.52936044652942,
                            40.30230752849768
                        ],
                        [
                            -83.52924865349425,
                            40.30230753872012
                        ],
                        [
                            -83.52924666169983,
                            40.3021800251207
                        ],
                        [
                            -83.52935848418728,
                            40.302181900418084
                        ],
                        [
                            -83.52936044652942,
                            40.30230752849768
                        ]
                    ]
                ]
            }
        }
    ]
}