我有以下代码:
$.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"
}...