2016-02-25 93 views
1

我要来加载在服务器无法解析一个JSON文件

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

这种情况正常工作

上传GeoJSON的文件,但我想在本地加载该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); 
+0

您的JSON有语法错误,这使得它无效json,这意味着它不能被解析。 –

+0

对不起,逗号不在那里这是一个错字错误。该守则本身没有逗号 –

回答

2

无效JSON是不解析的,邻可变bviously:

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

有疑问时,通过棉绒/格式化运行:

http://jsonlint.com/

你必须在JSON,一个逗号的错误,从年底的几个字符

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

或者这个很酷!

http://pro.jsonlint.com/

I am neither getting any error

也许周边代码被吞咽的误差。如果你把你的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

因为这是不正确的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]]]}}]}

这是正确的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 
         ] 
        ] 
       ] 
      } 
     } 
    ] 
}