JSON
{
"schools": [
{
"id": 1,
"name": "School A"
},
{
"id": 2,
"name": "School B"
}
],
"students": [
{
"id": 1,
"name": "Bobby",
"school": 1
}
]
}
How would I map the JSON into the following classes such that Bobby's school is mapped to the already instantiated School A.
public class School {
private Integer id;
private String name;
}
public class Student {
private Integer id;
private String name;
private School school;
}
I've tried some weird stuff with the Student class...
public class Student {
private Integer id;
private String name;
private School school;
@JsonProperty("school")
public void setSchool(Integer sid) {
for (School school : getSchools()) {
if (school.id == sid) {
this.school = school;
break;
}
}
}
}
The problem I'm having is that both the schools and the students are being parsed from the JSON at the same time, so I'm not sure how to get a list of the schools. Maybe I should parse these separately so I have the list of schools first?