简体   繁体   English

如何使用getJSON从文件中获取JSON数组?

[英]How to get JSON array from file with getJSON?

How could I get an array json of a json file with javascript and jquery 我怎么能用javascript和jquery获取json文件的数组json

I was triyng with the next code, with it doesnt work: 我对下一个代码感到满意,但它不起作用:

var questions = [];
function getArray(){
    $.getJSON('questions.json', function (json) {
        for (var key in json) {
            if (json.hasOwnProperty(key)) {
                var item = json[key];
                questions.push({
                    Category: item.Category
                });
            }
        }
        return questions;
    })
}

this is the json file called: questions.json 这是名为:questions.json的json文件

{
"Biology":{
    "Category":{
        "cell":{
            "question1":{
                "que1":"What is the cell?"
            },
            "option1":{
                "op1":"The cell is the basic structural and functional unit",
                "op2":"is a fictional supervillain in Dragon Ball"
            },
            "answer1":"opt1"
        }
    }
},
"Astronomy":{
    "Category":{
        "Mars":{
            "question1":{
                "que1":"How many moons does Mars?"
            },
            "option1":{
                "op1":"5",
                "op2":"2"
            },
            "answer1":"opt2"
        }
    }
}
}

I want to get an array with this format {Biology:{Category:{cell:{question1....}}}} 我希望得到一个这种格式的数组{生物学:{分类:{cell:{question1 ....}}}}

$.getJSON is an asynchronous function, so returning something inside that function does nothing, as it's not in scope, or received yet. $.getJSON是一个异步函数,因此返回该函数内部的任何内容都不起作用,因为它不在范围内,或者已经收到。 You should probably do something like: 你可能应该这样做:

function getArray(){
    return $.getJSON('questions.json');
}

getArray().done(function(json) {
    // now you can use json
    var questions = [];
    $.each(json, function(key, val) {
        questions[key] = { Category: val.Category };
    });
});

Your conditional within the for loop prevents anything from being added to your array. for循环中的条件可防止将任何内容添加到数组中。 Instead, check if your json object has the property, then get the value and add it to your array. 相反,检查您的json对象是否具有该属性,然后获取该值并将其添加到您的数组中。 In other words: 换一种说法:

if (questions.hasOwnProperty(key)) should be if (json.hasOwnProperty(key)) if (questions.hasOwnProperty(key))应该是if (json.hasOwnProperty(key))

Also, you can't simply return the result of an AJAX call like that, because the method runs asynchronously. 此外,您不能简单地return这样的AJAX调用的结果,因为该方法是异步运行的。 That return is actually applied to the inner success function callback, not getArray . return实际上应用于内部success函数回调,而不是getArray You have to use a callback pattern in order to pass the data only once it has been received, and operate on it accordingly. 您必须使用回调模式才能在收到数据后传递数据,并相应地对其进行操作。

(Of course since the array is defined in the outer scope you wouldn't have to return it anyway, but if you attempted to use it before the AJAX method ended it would be empty.) (当然,由于数组是在外部作用域中定义的,所以无论如何都不必返回它,但是如果你试图在AJAX方法结束之前使用它,那么它将是空的。)

Assuming you are going to render it to the DOM using a method called renderJSON : 假设您要使用名为renderJSON的方法将其呈现给DOM:

var questions = [];
function getArray(){
    $.getJSON('questions.json', function (json) {
        for (var key in json) {
            if (json.hasOwnProperty(key)) {
                var item = json[key];
                questions.push({
                    Category: item.Category
                });
            }
        }
        renderJSON(questions);
    });
}

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

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