gpt4 book ai didi

java - 将 JSON 数据解析为 Java 中的模型对象

转载 作者:行者123 更新时间:2023-11-30 06:54:29 24 4
gpt4 key购买 nike

我以前没有使用过 JSON 数据,所以才有这个问题。我在文件中有以下 JSON 对象。

{
"courses": [
{ "id":998", "name":"Java Data Structures", "teacherId":"375" },
{ "id":"999", "name":"Java Generics", "teacherId":"376" }

],
"teachers": [
{ "id":"375", "firstName":"Amiyo", "lastName":"Bagchi"},
{ "id":"376", "firstName":"Dennis", "lastName":"Ritchie"}
]
}

这是我的模型对象。

public class Course {

private int _id;
private String _name;
private Teacher _teacher;
}

public class Teacher {
private int _id;
private String _firstName;
private String _lastName;
}

我的任务是读取 JSON 对象并返回模型对象列表。

我已经导入了 jar 的 simple.JSON 系列,这是我读取文件的代码。

    FileReader reader = new FileReader(path);
JSONParser parser = new JSONParser();
Object obj = parser.parse(reader);

JSONObject jsonObject = (JSONObject) obj;

我的问题是,

  1. 如何将 JSON 文档解析为我的模型对象?
  2. 如果输入文件是 JSON 但格式不同,我该如何抛出异常/处理异常?

感谢任何帮助。

最佳答案

更新 我建议您使用 JSON 解析器来解析数据:

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
import java.util.HashMap;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;

class Course {

public int _id;
public String _name;
public Teacher _teacher;

private Course(int id, String name, Teacher teacher){
this._id = id;
this._name = name;
this._teacher = teacher;
}
public Course() {

}
}

class Teacher {
public int _id;
public String _firstName;
public String _lastName;
private Teacher(int id, String fname, String lname){
this._id = id;
this._firstName = fname;
this._lastName = lname;
}
public Teacher(){

}

}

public class jsontest {

public static void main(String[] args) throws JSONException, IOException {

// String JSON_DATA = "{\n"+
// " \"courses\": [\n"+
// " { \"id\":\"998\", \"name\":\"Java Data Structures\", \"teacherId\":\"375\" },\n"+
// " { \"id\":\"999\", \"name\":\"Java Generics\", \"teacherId\":\"376\" }\n"+
// "\n"+
// " ],\n"+
// " \"teachers\": [\n"+
// " { \"id\":\"375\", \"firstName\":\"Amiyo\", \"lastName\":\"Bagchi\"},\n"+
// " { \"id\":\"376\", \"firstName\":\"Dennis\", \"lastName\":\"Ritchie\"} \n"+
// " ]\n"+
// "}\n"+
// "";
// read json file into string
String JSON_DATA = new String(Files.readAllBytes(Paths.get("path_to_json_file")), StandardCharsets.UTF_8);

// using a JSON parser
JSONObject obj = new JSONObject(JSON_DATA);
// parse "teachers" first
List<Teacher> listCourses = new ArrayList<Teacher>();
List<JSONObject> listObjs = parseJsonData(obj,"teachers");
for (JSONObject c: listObjs) {
Teacher teacher = new Teacher();
teacher._id = c.getInt("id");
teacher._firstName = c.getString("firstName");
teacher._lastName = c.getString("lastName");
listCourses.add(teacher);
}
// parse "courses" next
List<Course> resultCourses = new ArrayList<Course>();
List<JSONObject> listObjs2 = parseJsonData(obj, "courses");

for (JSONObject c: listObjs2) {
Course course = new Course();
course._id = c.getInt("id");
course._name = c.getString("name");
int teacherId = c.getInt("teacherId");
HashMap<String, Teacher> map = new HashMap<String, Teacher>();
for (Teacher t: listCourses){
map.put(Integer.toString(t._id), t);
}
course._teacher = map.get(Integer.toString(teacherId));
resultCourses.add(course);
}
}


public static List<JSONObject> parseJsonData(JSONObject obj, String pattern)throws JSONException {

List<JSONObject> listObjs = new ArrayList<JSONObject>();
JSONArray geodata = obj.getJSONArray (pattern);
for (int i = 0; i < geodata.length(); ++i) {
final JSONObject site = geodata.getJSONObject(i);
listObjs.add(site);
}
return listObjs;
}

}

输出:

enter image description here

顺便说一句:示例中的 json 数据有一个值,其双引号不是成对的。要继续,它必须被修复。

关于java - 将 JSON 数据解析为 Java 中的模型对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36291420/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com