Question
What is the syntax to parse a JSON multidimensional array into a ActionScript class for a multiple choice app? This is how I currently have
What is the syntax to parse a JSON multidimensional array into a ActionScript class for a multiple choice app?
This is how I currently have my JSON data file
{
"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]
},
]
QuestionBank.as FILE
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, abc:questionArray[i].abc}); //HOW TO PARSE THE JSON DATA FROM ABOVE HERE } return b; } } } QuizGame.as File 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]){ //IS THIS RIGHT? 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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started