Hi, im using gson to parse a json file but i dont know how to handle if ONE of the objects inside the array doesnt have all the properties it should. should i just do a for loop and check if any property is null or is there a better way to do it?
this is some my code:
System.out.println("What is the name of the event file?");
filename = scan.nextLine();
try {
JsonReader reader = new JsonReader(new FileReader(filename));
events = gson.fromJson(reader, Events.class);
events.getEvents()[0].printEvent();
} catch (IOException ioe) {
System.out.println("The file " + filename + " could not be found.");
} catch (JsonSyntaxException e) {
System.out.println("The file " + filename + " is not formatted properly.");
}
}
and this is the json
{
"data": [
{
"name": "P!NK",
"tour": "Summer Carnival 2023 Tour",
"localDate": "2023-10-05",
"venue": "SoFi Stadium"
},
{
"name": "Eagles",
"tour": "Hotel California 2023 Tour",
"localDate": "2023-02-24",
"venue": "Acrisure Arena"
},
events is an array of the class event, which is:
public class Event {
private String name;
private String tour;
private String localDate;
private String venue;
} // getters and printevent which basically prints all variables here
so for exmaple, if i remove venue from the first one, printEvent would print the correct info except for venue, which prints null
as i asked before, is checking if there is any null property the only way to do it or is there an easier way to immediately call the JsonSyntaxException or is there any other exception?