简体   繁体   English

将JSON数据保存到数组中

[英]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. 有什么方法可以从JSON文件获取数据并将其保存到JavaScript中的多维数组中。 The JSON file is a .php file with header set to JSON. JSON文件是一个.php文件,其标头设置为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). 在您的示例中, questionJ被视为一个字符串,并且没有看到setArray()函数,很难确定到底是什么问题(例如,我不知道您是否正在解析此函数内部的字符串)。

However, assuming that you wish for questionJ to be an array of arrays, the following should work: 但是,假设您希望questionJ是一个数组数组,则应该可以执行以下操作:

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. 注意,在$.each函数内部,您可以引用this ,它引用当前迭代的项目。 Using this method (with push() and the array data structure) directly, the datatype is also preserved from the JSON. 直接使用此方法(通过push()和数组数据结构),该数据类型也从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. 另外,由于您没有直接与代码段内的DOM进行交互,因此无需将其包装在DOMReady处理程序内。

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 编辑: BenM更快

Edit: 编辑:

This works as well, but I cannot use the array outside of $.getJSON 这也可以,但是我不能在$ .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. 由于javascript是一种异步语言,因此控制台日志调用发生在请求json之后的推送之前。 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). 函数setTimeout只需等待3秒钟,然后运行该函数(与json请求非常相似)。

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. 编辑:将链接更改为其他更好的页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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