【发布时间】:2017-04-26 04:33:30
【问题描述】:
JSON
{
"schools": [
{
"id": 1,
"name": "School A"
},
{
"id": 2,
"name": "School B"
}
],
"students": [
{
"id": 1,
"name": "Bobby",
"school": 1
}
]
}
我如何将 JSON 映射到以下类中,以便将 Bobby 的学校映射到已经实例化的 School A。
public class School {
private Integer id;
private String name;
}
public class Student {
private Integer id;
private String name;
private School school;
}
我在 Student 类中尝试了一些奇怪的东西...
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;
}
}
}
}
我遇到的问题是同时从 JSON 中解析学校和学生,所以我不确定如何获取学校列表。也许我应该分别解析这些,以便我首先获得学校列表?
【问题讨论】:
-
你试过了吗?目前尚不清楚您遇到了什么问题。
-
很抱歉,我试图通过一个我一直在尝试采用的方法的示例来更好地解释我试图解决的问题。