【发布时间】:2015-07-15 17:14:10
【问题描述】:
我有以下代码:
$.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"
}...
【问题讨论】:
标签: javascript jquery json random