簡體   English   中英

無法解析Json文件

[英]Not able to Parse a Json File

我想加載服務器上載的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);

如有疑問,請通過lint / 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

不像短絨布那樣方便,但是至少您會看到一個錯誤。

無效的JSON是不可解析的,顯然:

...snip...[ -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]]]}]}}

這是正確的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
                        ]
                    ]
                ]
            }
        }
    ]
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM