javascript - 从多个文件加载多个 JSON 对象
问题描述
我正在尝试创建一个测验网站。测验数据(问题、答案和正确答案)存储在 JSON 文件中。一切都按原样工作,但我想在每个单独的 JSON 文件中包含一个唯一的图像。我认为最好的方法是创建另一个对象。这意味着我将具有如下所示的结构:
[
{"adImage" : "images/NoOvertake.jpg"}
],
[
{
"question" : "Before making a U - turn in the road you should always:",
"answers":[
{"id" : 0, "text" : "Select a higher gear than normal"},
{"id" : 1, "text" : "Signal so that other drivers can slow down"},
{"id" : 2, "text" : "Look over your shoulder for final confirmation"},
{"id" : 3, "text" : "Give another signal as well as using your indicators"}
],
"correct" : [2],
"allAns":[]
},
{
"question" : "As a driver what do you understand by the term 'Blind Spot'?",
"answers" : [
{"id" : 0, "text" : "An area covered by your left hand mirror" },
{"id" : 1, "text" : "An area not covered by your headlights" },
{"id" : 2, "text" : "An area covered by your right hand mirror" },
{"id" : 3, "text" : "An area not covered by your mirrors" }
],
"correct" : [3],
"allAns":[]
}
]
这是在我在所有问题之上添加新图像对象之前曾经工作的 JavaScript:
var app = angular.module('myQuiz',[]);
app.controller('QuizController'
['$scope','$http','$q','$sce',function($scope,$http,$q,$sce){
var jsonData = ['alertness','attitude', 'safety and your vehicle',
'safety margins','hazard awareness',
'vulnerable road users','other type of vehicles',
'vehicle handling','dual carriageway rules',
'rules of the road','road and traffic signs',
'documents','accidents','vehicle loading'];
var promise = [];
$scope.allQuestions = [];
for(var i=0;i<jsonData.length;i++) {
promise.push($http.get(jsonData[i]+'.json'))
}
$q.all(promise).then(function(quizData){
for(var i=0;i<quizData.length;i++) {
$scope.allQuestions[i] = {};
$scope.allQuestions[i].quizName = jsonData[i];
$scope.allQuestions[i].data = quizData[i].data;
$scope.allQuestions[i].score = 0;
$scope.allQuestions[i].activeQuestion = -1;
$scope.allQuestions[i].activeQuestionAnswered = 0;
$scope.allQuestions[i].percentage = 0;
var questionNumber = quizData.length;
}
});
]);
现在,连问题都不会出现。我感谢任何形式的帮助,甚至是替代解决方案。我需要做的就是添加一个图像,它会为每个问题保留在那里。我需要什么 HTML 代码来显示图像?
提前致谢!
解决方案
一个有效的 JSON 对象只有一个根元素。您可以使用 JSON linter 查看您的 JSON 是否有效http://jsonlint.com。我建议使用类似这样的结构。
{
"adImage": "images/NoOvertake.jpg",
"questions": [
{
"question": "Before making a U - turn in the road you should always:",
"answers": [
{
"id": 0,
"text": "Select a higher gear than normal"
},
{
"id": 1,
"text": "Signal so that other drivers can slow down"
},
{
"id": 2,
"text": "Look over your shoulder for final confirmation"
},
{
"id": 3,
"text": "Give another signal as well as using your indicators"
}
],
"correct": [
2
],
"allAns": []
},
{
"question": "As a driver what do you understand by the term 'Blind Spot'?",
"answers": [
{
"id": 0,
"text": "An area covered by your left hand mirror"
},
{
"id": 1,
"text": "An area not covered by your headlights"
},
{
"id": 2,
"text": "An area covered by your right hand mirror"
},
{
"id": 3,
"text": "An area not covered by your mirrors"
}
],
"correct": [
3
],
"allAns": []
}
]
}
推荐阅读
- java - 我收到错误,因为尝试在空对象引用上调用虚拟方法 'void android.widget.TextView.setText(java.lang.CharSequence)'
- ios - 找不到 IOS 12.5.4 (16H50) 的设备支持文件
- azure - 如何使用机器学习从给定值中找到键
- spring-boot - 带有 @Restcontoller 计时问题的 Spring 集成流程
- python - 我在字母之间而不是单词之间用逗号得到单词
- highcharts - Highchart-export-server 未在图表上正确显示绘图带
- c# - 为什么我的炸弹撞击半径和爆炸效果在不同的位置实例化?
- kubernetes - 无法在 tekton 管道中推送 AWS ECR 图像
- r - read_html() 没有在 R 中获取 NES 股票网站的 HTML
- react-native - 需要使用可重复使用的开关创建一个切换组件