将JSON文件转换为HTML广播测验



我需要获取JSON文件,排序它的问题,可能的答案,然后显示正确的答案,但我不能获取和排序它正确

<script>
fetch("quiz.json").then(function (response) {
response.json()
}).then(function(quiz){
for(let i = 0; 1 <quiz.length; i++){
document.body.innerHTML += '<h2>' +quiz[i].question + '</h2>';
document.body.innerHTML += '<imput type="radio">' +quiz[i].options ;
document.body.innerHTML += '<p>' +quiz[i].answer + '</p>';
}
})
</script>

当我尝试时,它显示">

Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'length')
at (index):30:40
```"
JSON file : 

{"quiz" {"q1" {问题:哪一个是正确的NBA球队名?"options"("纽约公牛";"洛杉矶国王";金州勇士队;"休斯顿Rocket"],答案";休斯顿火箭"},"q2" {"合十礼"是亚洲哪一种语言的传统问候方式?"options"("Hindi","Mandarin","Nepalese","Thai"],"answer"Hindi"},"q3" {施普雷河流经欧洲哪个主要的首都?"options"("Berlin","Paris","Rome","London"],"answer"Berlin"},"q4" {问:哪位著名艺术家既经历了"玫瑰时期"又经历了"蓝色时期"?"options"("Pablo Picasso",文森特·梵高;"萨尔瓦多Dali","埃德加Degas"],回答";毕加索"}}}


Also Im using XAMPP and only vanilla js

尝试将数据更改为

{ "quiz": [
{ "question": "Which one is correct team name in NBA?", "options": [ "New York Bulls", "Los Angeles Kings", "Golden State Warriros", "Huston Rocket" ], "answer": "Huston Rocket" },

{ "question": "'Namaste' is a traditional greeting in which Asian language?", "options": [ "Hindi", "Mandarin", "Nepalese", "Thai" ], "answer": "Hindi" }, 

{ "question": "The Spree river flows through which major European capital city?", "options": [ "Berlin", "Paris", "Rome", "London" ], "answer": "Berlin" }, 

{ "question": "Which famous artist had both a 'Rose Period' and a 'Blue Period'?", "options": [ "Pablo Picasso", "Vincent van Gogh", "Salvador Dalí", "Edgar Degas" ], "answer": "Pablo Picasso" } 
] 
}

将使quiz成为一个数组,然后你可以用你的代码导航它。

最新更新