Question: How to format a multidimensional JSON array file that will have a question, multiple choice a, b, or c, and an answer e.g. a to
How to format a multidimensional JSON array file that will have a question, multiple choice a, b, or c, and an answer e.g. a to be used in a separate ActionScript Classes QuestionBank.as & QuizGame.as
JSON DATA 1/20 questions - IF the below JSON Data is correct then how can I use it in the classes below?
{
"questions": [
{
"q": ["For Socrates, an unexamined life is a tragedy because it results in grievous harm to _____."],
"choices": ["A.the soul", "B.the body", "C.the state"],
"abc": [0]
},
]
THE ABOVE DATA NEEDS TO BE parsed I believe the term is into
QuestionBank.as
package com.robertosborn {
import flash.events.EventDispatcher;
import flash.net.URLRequest;
import flash.net.URLLoader;
import flash.events.Event;
public class QuestionBank extends EventDispatcher {
private const JSON_URL:String = "data/questionsTEST.json";
private var jsonRequest:URLRequest;
private var jsonLoader:URLLoader;
private var questionArray:Array;
public function QuestionBank() {
questionArray = new Array();
loadJSON();
}
private function loadJSON():void {
jsonRequest = new URLRequest(JSON_URL);
jsonLoader = new URLLoader;
jsonLoader.addEventListener(Event.COMPLETE, jsonLoaded);
jsonLoader.load(jsonRequest);
}
private function jsonLoaded(e:Event):void {
var jsonData:Object = JSON.parse(e.target.data);
for each (var question:Object in jsonData.questions){
questionArray.push(question);
}
dispatchEvent(new Event(Event.COMPLETE));
}
public function buildBank():Array {
var b:Array = new Array();
var max:int = questionArray.length;
for(var i:int=0; i
b.push({q:questionArray[i].q, choices:questionArray[i].choices, abc:questionArray[i].abc});
}
return b;
}
}
}
QuizGame.as
package com.robertosborn {
import flash.display.MovieClip;
import flash.text.TextField;
import flash.display.SimpleButton;
import flash.events.Event;
import com.robertosborn.QuestionBank;
import flash.events.MouseEvent;
import flash.utils.setTimeout;
public class QuizGame extends MovieClip {
public var aBtn:SimpleButton;
public var bBtn:SimpleButton;
public var cBtn:SimpleButton;
public var questionText:TextField;
public var statusMsg:TextField;
private var questionBank:QuestionBank;
private var questionArray:Array;
private const questionMax:int = 5;
private var questionCount:int = 0;
private var currentAnswer:String;
private var currentScore:int = 0;
public function QuizGame() {
statusMsg.text = "";
questionArray = new Array();
questionBank = new QuestionBank();
questionBank.addEventListener(Event.COMPLETE, dataReady);
aBtn.addEventListener(MouseEvent.CLICK, aPressed);
bBtn.addEventListener(MouseEvent.CLICK, bPressed);
cBtn.addEventListener(MouseEvent.CLICK, cPressed);
}
private function dataReady(e:Event):void {
questionArray = questionBank.buildBank();
grabQuestion();
}
private function grabQuestion():void {
if(questionCount < questionMax) {
var max:int = questionArray.length-1;
var rq:int = Math.floor(Math.random() * (max - 0 + 1));
var ta:Array = questionArray.splice(rq, 1);
questionText.text = ta[0].q;
currentAnswer = ta[0].abc;
questionCount++;
} else {
gameOver();
}
}
private function gameOver():void {
statusMsg.text = currentScore+"/100 points";
questionText.text = "Game Over!";
}
private function aPressed(e:MouseEvent):void {
if(statusMsg.text == ""){
if(currentAnswer == "abc":[0]){
currentScore += 100/questionMax;
statusMsg.text = "CORRECT!";
}else{
statusMsg.text = "WRONG";
}
setTimeout(newQuestion, 1000);
}
}
private function bPressed(e:MouseEvent):void {
if(statusMsg.text == ""){
if(currentAnswer == "abc":[1]){
currentScore += 100/questionMax;
statusMsg.text = "CORRECT!";
}else{
statusMsg.text = "WRONG";
}
setTimeout(newQuestion, 1000);
}
}
private function cPressed(e:MouseEvent):void {
if(statusMsg.text == ""){
if(currentAnswer == "abc":[2]){
currentScore += 100/questionMax;
statusMsg.text = "CORRECT!";
}else{
statusMsg.text = "WRONG";
}
setTimeout(newQuestion, 1000);
}
}
private function newQuestion():void {
statusMsg.text = "";
grabQuestion();
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
