I'm new to JSON and I'm trying to parse my JSON File into Java, and it works, but I've got Objects/Arrays that contains more arrays and I don't know how to iterate it correctly. This is my JSON File:
{
"quiz": {
"sport": {
"q1": {
"question": "Which one is correct team name in NBA?",
"options": [
"New York Bulls",
"Los Angeles Kings",
"Golden State Warriros",
"Huston Rocket"
],
"answer": "Huston Rocket"
}
},
"maths": {
"q1": {
"question": "5 + 7 = ?",
"options": [
"10",
"11",
"12",
"13"
],
"answer": "12"
},
"q2": {
"question": "12 - 8 = ?",
"options": [
"1",
"2",
"3",
"4"
],
"answer": "4"
}
}
}
}
I don't know how I can get all data in this structure. I mean that I know it's a quiz and the two topics are sport and maths and that each topic has questions and an answer.
I would like to have each value avaiable.
This is my Java Code
JSONParser jsonParser = new JSONParser();
try
{
JSONArray a = (JSONArray) jsonParser.parse("pathToFile");
for (Object o : a)
{
JSONObject task1 = (JSONObject) o;
String name = (String) task1.get("quiz");
System.out.println(name);
String topic = (String) task1.get("sport");
System.out.println(topic);
}
}catch (ParseException e)
{
e.printStackTrace();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
Can someone explain the logic to me?
Assuming you don't want to use GSON and let loose your own logic on handling the quiz objects:
Analyze the JSON and create them logical objects for the values.
Then when you read the JSON, you create the objects as required and attach them as needed.
You'd need a Quiz, Category, Question and Option objects.
then your code would become something like this(psuedo code, not tested if it actually works):
public Quiz createQuiz(JSONObject raw)
{
Quiz quiz = new Quiz();
Iterator<String> keys = raw.keys();
while(keys.hasNext()) {
String key = keys.next();
if (raw.get(key) instanceof JSONObject) {
JSONObject rawCategory = (JSONObject)raw.get(key);
quiz.addCategory(this.createCategory(key, rawCategory));
}
}
return quiz;
}
public Category createCategory(String name, JSONObject raw)
{
Category category = new Category(name);
Iterator<String> keys = raw.keys();
while(keys.hasNext()) {
String key = keys.next();
if (raw.get(key) instanceof JSONObject) {
JSONObject rawQuestion = (JSONObject)raw.get(key);
category.addQuestion(this.createQuestion(key, rawQuestion));
}
}
return category;
}
public Category createQuestion(String name, JSONObject raw)
{
Question question = new Question(name);
if(raw.hasKey("question") && raw.get("question") instanceof String) {
question.setQuestionText((String) raw.get("question"));
}
if(raw.hasKey("answer") && raw.get("answer") instanceof String) {
question.setAnswerText((String) raw.get("answer"));
}
if(raw.hasKey("options") && raw.get("options") instanceof JSONArray) {
JSONArray rawOptions = (JSONArray)raw.get("options");
for(Object rawOption : rawOptions) {
if(rawOption instanceof String) {
question.addOption(this.createOption((String) rawOption));
}
}
}
return question;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With