Question: How to parse JSON data from multidimensional array with the objects 'q' is the Question - 'choices' - 'abc' is correct Answer? into ActionScript QuestionBank.as
How to parse JSON data from multidimensional array with the objects 'q' is the Question - 'choices' - 'abc' is correct Answer? into ActionScript QuestionBank.as class?
QuestionBank.as Class 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, choices:questionArray[i].choices, abc:questionArray[i].abc}); //ERROR here I believe } return b; } } }
-------THIS IS HOW I CURRENTLY HAVE THE JSON DATA FORMATTED BUT NOT SURE IF CORRECT (2 questions with choices and answer)
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]
},
{
"q": ["A question-and-answer dialogue in which propositions are methodically scrutinized to uncover the truth is known as _____."],
"choices": ["A.an argument", "B.the Socratic method", "C.a debate"],
"abc": [1]
}
]
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
