0

I try to load and add a local json file as var for a futur use in my script. I think that the data aren't load when I try to populate my var with the json data but fail to find the solution!

Here is my json file (it's a geojson file but may work as a 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
                        ]
                    ]
                ]
            }
        }
    ]
}

And my script

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

I also try

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

In the console the json is load so I guest it's a sync/async problem...

henri_1310
  • 315
  • 7
  • 21
  • It's an async issue...you need to use a callback - or do your logic inside the callback from the `getJSON`. – tymeJV Dec 16 '14 at 15:55
  • You've placed the `console.log` outside the callback, so you're trying to log the values before the data is available, see the duplicate post for a thourough explanation on how it works. – adeneo Dec 16 '14 at 15:56
  • Thanks for your comment. I finally find the solution with the success callback like this jQuery.getJSON("/datas/testze.json", function(data) { }) .success(function(data) { var geojson = data; console.log(geojson); }); – henri_1310 Dec 16 '14 at 16:04

0 Answers0