0

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.

1
  • In your example, questionJ is a string. Please show us your setArray() function. Commented Jun 10, 2016 at 16:42

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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

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.

7 Comments

Thank you for your answer, it works, but only with the array questionJSONS declared inside $.getJSON, and I can not use it outside of $.getJSON, which I actually need. P.S this is my first time using stackoverflow to ask questions so I may have problems formatting stuff right :p.
@BesnikCani You can remove the var definition in front of questionsJSONS to turn it into a fully-scoped variable. That should resolve your issue.
it still does not work, when I console.log it, it just shows two empty square brackets, when I console.log it inside the $.getJSON, it prints the actual array.
Can you share what the JSON that is returned by your test2.php file?
Do you need what the test2.php file contains or what it returns when we call it with getJSON. Because I get the data that I need, I just cannot use them outside of the getJSON method.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.