简体   繁体   中英

Saving JSON data into an array

Is there any way I can obtain the data from a JSON file and save them into a multidimensional array in JavaScript. The JSON file is a .php file with header set to JSON.

I've tried so far:

 var questionJSONS="";
$(document).ready(function(){
        var questionJ="";
        $.getJSON("http://localhost:8080/audioMillionaire/test2.php",function(result){
        $.each(result, function(i, item){
questionJ+= "['"+result[i].qid+"','"+result[i].description+"','"+result[i].a+"']";
  });
        setArray(questionJ);       
});

});

And many other things, but none of them seem to work.

In your example, questionJ is being treated as a string, and without seeing your setArray() function, it's difficult to say for sure what's wrong (I don't know if you're parsing the string inside this function, for example).

However, assuming that you wish for questionJ to be an array of arrays, the following should work:

questionJSONS = [ ];

$.getJSON('http://localhost:8080/audioMillionaire/test2.php', function(result){
    $.each(result, function() {
        questionJSONS.push( [ this.qid, this.description, this.a ] );
    });
});

Notice that inside the $.each function, you can reference this , which refers to the currently iterated item. Using this method (with push() and the array data structure) directly, the datatype is also preserved from the JSON.

Also, since you're not directly interacting with the DOM inside of the snippet, there's no requirement to wrap it inside the DOMReady handler.

I think this is what you meant:

$(document).ready(function() {
    var questions = [];

    $.getJSON("http://localhost:8080/audioMillionaire/test2.php", function(result) {
        $.each(result, function(i, item) {
            questions.push([result[i].qid, result[i].description, result[i].a]);
        });
    });
});

If not, please comment.

Edit: BenM was faster

Edit:

This works as well, but I cannot use the array outside of $.getJSON

This is because you are probably using code like this:

...
        questions.push([result[i].qid, result[i].description, result[i].a]);
    });
});

console.log(questions);

Because javascript is an asynchronous language the console log call happens before the pushes after the json was requested. You can read more about it here .

$(document).ready(function() {
    console.log("set test to old");
    var test = "old";

    setTimeout(function() {
        console.log("set test to new");
        test = "new";
        console.log("inside timeout: " + test);
    }, 3000); // runs in 3 seconds


    console.log("outside timeout: " + test);
});

This code should provide a good example. The function setTimeout just waits 3 seconds and runs the function (much like the json request).

Knowing this you should find a solution on your own (like calling a passed function after the array has been pushed). If not comment.

Edit: changed link to other, better page.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM