Mar*_*e98 5 java arrays json file object
我是 JSON 的新手,我正在尝试将我的 JSON 文件解析为 Java,并且它可以工作,但是我有包含更多数组的对象/数组,我不知道如何正确迭代它。这是我的 JSON 文件:
{
"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"
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我不知道如何获取此结构中的所有数据。我的意思是我知道这是一个测验,两个主题是运动和数学,每个主题都有问题和答案。
我希望每个值都可用。
这是我的 Java 代码
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();
}
Run Code Online (Sandbox Code Playgroud)
有人可以向我解释逻辑吗?
首先,你需要创建类,比较对象更容易
此链接可以帮助您将 json 转换为类
https://www.site24x7.com/tools/json-to-java.html
它们是生成的类
/* * 要更改此许可证标头,请在项目属性中选择许可证标头。* 要更改此模板文件,请选择“工具”|“ 模板 * 并在编辑器中打开模板。*/ 包 com.examen.overflow;
/**
*
* @author wilso
*/
public class Dto {
private Quiz quiz;
public Quiz getQuiz() {
return quiz;
}
public void setQuiz(Quiz quiz) {
this.quiz = quiz;
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.examen.overflow;
/**
*
* @author wilso
*/
public class Maths {
private Question q1;
private Question q2;
// Getter Methods
public Question getQ1() {
return q1;
}
public Question getQ2() {
return q2;
}
// Setter Methods
public void setQ1(Question q1) {
this.q1 = q1;
}
public void setQ2(Question q2) {
this.q2 = q2;
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.examen.overflow;
import java.util.List;
/**
*
* @author wilso
*/
public class Question {
private String answer;
private List<String> options;
private String question;
public String getAnswer() {
return answer;
}
public void setAnswer(String answer) {
this.answer = answer;
}
public List<String> getOptions() {
return options;
}
public void setOptions(List<String> options) {
this.options = options;
}
public String getQuestion() {
return question;
}
public void setQuestion(String question) {
this.question = question;
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.examen.overflow;
/**
*
* @author wilso
*/
public class Quiz {
private Sport sport;
private Maths maths;
public Sport getSportObject() {
return sport;
}
public void setSportObject(Sport sport) {
this.sport = sport;
}
public Maths getMathsObject() {
return maths;
}
public void setMathsObject(Maths maths) {
this.maths = maths;
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.examen.overflow;
/**
*
* @author wilso
*/
public class Sport {
Question q1;
// Getter Methods
public Question getQ1() {
return q1;
}
// Setter Methods
public void setQ1(Question question) {
this.q1 = question;
}
}
Run Code Online (Sandbox Code Playgroud)
当你上完课后,你只需要做下一个
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.examen.overflow;
import com.google.gson.Gson;
public class Convert {
public static void main(String[] args) {
String json = "{\n"
+ " \"quiz\": {\n"
+ " \"sport\": {\n"
+ " \"q1\": {\n"
+ " \"question\": \"Which one is correct team name in NBA?\",\n"
+ " \"options\": [\n"
+ " \"New York Bulls\",\n"
+ " \"Los Angeles Kings\",\n"
+ " \"Golden State Warriros\",\n"
+ " \"Huston Rocket\"\n"
+ " ],\n"
+ " \"answer\": \"Huston Rocket\"\n"
+ " }\n"
+ " },\n"
+ " \"maths\": {\n"
+ " \"q1\": {\n"
+ " \"question\": \"5 + 7 = ?\",\n"
+ " \"options\": [\n"
+ " \"10\",\n"
+ " \"11\",\n"
+ " \"12\",\n"
+ " \"13\"\n"
+ " ],\n"
+ " \"answer\": \"12\"\n"
+ " },\n"
+ " \"q2\": {\n"
+ " \"question\": \"12 - 8 = ?\",\n"
+ " \"options\": [\n"
+ " \"1\",\n"
+ " \"2\",\n"
+ " \"3\",\n"
+ " \"4\"\n"
+ " ],\n"
+ " \"answer\": \"4\"\n"
+ " }\n"
+ " }\n"
+ " }\n"
+ "}";
Dto quiz = new Gson().fromJson(json, Dto.class);
System.out.println(quiz);
//Iterate object
for (String option : quiz.getQuiz().getMathsObject().getQ1().getOptions()) {
System.out.println(option);
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
101 次 |
| 最近记录: |