使用getJSON在javascript中获取json文件作为变量

时间:2014-12-16 15:54:47

标签: javascript jquery ajax json getjson

我尝试加载并添加一个本地json文件作为var,以便在我的脚本中使用未来。我认为当我尝试使用json数据填充我的var但未能找到解决方案时,数据不会加载!

这是我的json文件(它是一个geojson文件但可以作为json工作):

{
    "type": "FeatureCollection",
    "features": [
        {
            "type": "Feature",
            "id": "45140",
            "properties": {
                "Name": "Nogent-le-Rotrou",
                "ZE": "0054",
                "Chomage2013": 8.7,
                "Chomage2012": 8.3,
                "Chomage2011": 7.5,
                "Chomage2010": 7.6,
                "Chomage2009": 8.2,
                "Chomage2008": 6.2,
                "Chomage2007": 5.7,
                "Chomage2006": 6.8,
                "Chomage2005": 7.4,
                "Chomage2004": 7.2,
                "Chomage2003": 6.4,
                "evochomage2013": 4.82
            },
            "geometry": {
                "type": "Polygon",
                "coordinates": [
                    [
                        [
                            0.93175560354212,
                            48.474513031284
                        ],
                        [
                            0.93570906113333,
                            48.475508748811
                        ],
                        [
                            0.93824280719487,
                            48.469235350982
                        ],
                        [
                            0.94065612469203,
                            48.459117109658
                        ],
                        [
                            0.95557675746743,
                            48.445300194094
                        ]
                    ]
                ]
            }
        },
        {
            "type": "Feature",
            "id": "45138",
            "properties": {
                "Name": "Le Blanc",
                "ZE": "2407",
                "Chomage2013": 7.6,
                "Chomage2012": 7.2,
                "Chomage2011": 7,
                "Chomage2010": 7.1,
                "Chomage2009": 6.8,
                "Chomage2008": 5.4,
                "Chomage2007": 5.7,
                "Chomage2006": 6.4,
                "Chomage2005": 6.5,
                "Chomage2004": 6.7,
                "Chomage2003": 6.3,
                "evochomage2013": 5.56
            },
            "geometry": {
                "type": "Polygon",
                "coordinates": [
                    [
                        [
                            0.93775810435161,
                            46.594424656767
                        ],
                        [
                            0.91586530805381,
                            46.596631487525
                        ],
                        [
                            0.90498141613517,
                            46.617645541724
                        ],
                        [
                            0.89405928709303,
                            46.628623597551
                        ],
                        [
                            0.89551763798088,
                            46.631821020526
                        ],
                        [
                            0.90762804417877,
                            46.646715861732
                        ],
                        [
                            0.91563786082172,
                            46.650671906043
                        ],
                        [
                            0.90733980986345,
                            46.666436781446
                        ],
                        [
                            0.911236562269,
                            46.676028593016
                        ],
                        [
                            0.90226151243206,
                            46.677666264493
                        ],
                        [
                            0.91815499060708,
                            46.690484998251
                        ],
                        [
                            0.92713345915642,
                            46.693601953386
                        ],
                        [
                            0.92481246298246,
                            46.700216357902
                        ],
                        [
                            0.91040415839575,
                            46.717410974559
                        ],
                        [
                            0.91117187498138,
                            46.727071705034
                        ],                            [
                            0.85872947708123,
                            46.75915353208
                        ],
                        [
                            0.84484012327248,
                            46.763019155912
                        ],
                        [
                            0.82939560079849,
                            46.776869974366
                        ],
                        [
                            0.82609906707742,
                            46.786823854041
                        ],
                        [
                            0.81335360224109,
                            46.791680250747
                        ],
                        [
                            0.81559729294859,
                            46.804999772692
                        ],
                        [
                            0.81016241531648,
                            46.814191974454
                        ],
                        [
                            0.80985732688176,
                            46.815897559621
                        ],
                        [
                            0.96243716570588,
                            46.572537159179
                        ],
                        [
                            0.95559838465873,
                            46.577612496963
                        ],
                        [
                            0.94136729105315,
                            46.580879388346
                        ],
                        [
                            0.93568777424923,
                            46.588721686133
                        ],
                        [
                            0.93775810435161,
                            46.594424656767
                        ]
                    ]
                ]
            }
        }
    ]
}

我的剧本

jQuery.getJSON("/datas/testze.json", function(data) {
    var geojson = data;
});  
console.log(geojson);   

我也试试

jQuery.getJSON("/datas/testze.json?jsoncallback=?", function(data) {
    var geojson = data;
});  
console.log(geojson); 

在控制台中,json正在加载,所以我访问它是同步/异步问题......

0 个答案:

没有答案