2016-02-25 56 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 
         ] 
        ] 
       ] 
      } 
     } 
    ] 
}