I am creating a program whereby I need to save(write) objects to a JSON, and then load(read) these objects back into the program. I have told that GSON is a good way to do this. However, I am having some troubles with it. I have tried the following:
Gson g = new Gson();
try (FileWriter writer = new FileWriter(file)) {
String j = g.toJson(board);
writer.write(j);
writer.flush();
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
When I run the program, I get the error:
class object3 declares multiple JSON fields named state
The object I am trying to write to the JSON has an ID, and an Array of another Object (object2
), and each of these objects have an Array of another Object (object3
). object3
has multiple fields, mostly strings. What is the simplest way for me to write an object such as this to a JSON file and then be able to read it back into my program?