如何使用 javascript 和 jquery 获取 json 文件的数组 json
我正在尝试使用下一个代码,但它不起作用:
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;
})
}
这是名为的 json 文件: questions.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"
}
}
}
}
我想得到一个具有这种格式的数组 {Biology:{Category:{cell:{question1....}}}}