0

I want to parse json from local file by:

$.getJSON("test.json", function(json) {
    console.log(json); 
});

But how to use object json in this case out of $.getJSON()?

For example, if the code is:

$.getJSON("test.json", function(json) {
    //console.log(json); 
});
console.log(json); 

it will not show json in console. How to solve this?

I searched and tried as below:

                function getJsonData() {
                    $.getJSON("test.json", function (jsondata) {
                        callback(jsondata);
                    });
                }


                function callback(jsondata) {
                   //do something with the data
                    console.log(var1);
                    return var1;  //return the variable var1

                }


                var var1= getJsonData();
                console.log(var1);

But what's in the console is undefined.

Elva
  • 399
  • 2
  • 7
  • 22

0 Answers0