Come potrei ottenere un JSON serie di un file JSON con javascript e jqueryCome ottenere l'array JSON dal file con getJSON?
ero triyng con il codice successivo, con esso non lavorare:
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;
})
}
questo è il file JSON chiamato: domande. 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"
}
}
}
}
voglio ottenere un array con questo formato {Biologia: {Categoria: {cella: {question1 ....}}}} entro
Grazie. Funziona! –
@chikatetsu - prego! – adeneo
A proposito, come posso ottenere il valore per esempio di Astronomia ?? console.log (domande [1]. [Astronomia]), in questo modo è ok? –