У меня есть следующий код:Выберите случайный объект из JSON
$.getJSON('js/questions1.json').done(function(data){
window.questionnaire = data;
console.log(window.questionnaire);
startGame();
});
Это приносит JSON от сервера и журналов его в переменную. Теперь после этого, я хочу, чтобы выбрать случайный вопрос, расположенный в questions.json документа:
function pickRandomQuestion(){
window.selectedquestion = window.questionnaire[Math.floor(Math.random * window.questionnaire.length)];
console.log(window.selectedquestion);
console.log(window.questionnaire);
}
Однако, когда console.log()
переменную selectedquestion, ничего не возвращается, это не определено. Что-то не так с моим кодом? Я втрое проверил его, и я не вижу в этом ничего плохого, но это может быть только моя голова, играющая со мной.
Вот как выглядит JSON:
"q1" : {
"question" : "This country is one of the largest wine-producing countries of the world, where wine is grown in every region of the country. Which country is this?",
"a" : "France",
"b" : "Italy",
"c" : "Germany",
"d" : "Australia",
"corrrect" : "b"
},
"q2" : {
"question" : "What is the name for the type of art portrait that deliberately exaggerates a person?",
"a" : "Environmental",
"b" : "Cartooning",
"c" : "Caricature",
"d" : "Tribal",
"corrrect" : "c"
},
"q3" : {
"question" : "Who was the first president of the United States?",
"a" : "Abraham Lincoln",
"b" : "Ronald Reagan",
"c" : "George Washington",
"d" : "Barack Obama",
"corrrect" : "c"
}...
Это сработало! Благодаря! – codeninja