2016-12-11 47 views
3

JSON反序列化与杰克逊参照现有对象

{ 
    "schools": [ 
    { 
     "id": 1, 
     "name": "School A" 
    }, 
    { 
     "id": 2, 
     "name": "School B" 
    } 
    ], 
    "students": [ 
    { 
     "id": 1, 
     "name": "Bobby", 
     "school": 1 
    } 
    ] 

} 

如何将我的JSON映射到以下类,鲍比的学校被映射到已实例化的学校A.

public class School { 
    private Integer id; 
    private String name; 
} 

public class Student { 
    private Integer id; 
    private String name; 
    private School school; 
} 

我试过一些奇怪的东西与学生类...

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中解析出来,所以我不确定如何获得学校名单。也许我应该分开解析这些,所以我先列出学校名单?

+0

您尝试过什么吗?目前尚不清楚你所困扰的是什么。 – 4castle

+0

对不起,我试图更好地解释我试图采取的一种方法的例子,我试图解决的问题。 – Rawr

回答

1

杰克逊会为你做。只需用@JsonIdentityInfo注释您的对象:

@JsonIdentityInfo(scope=School.class, generator=ObjectIdGenerators.PropertyGenerator.class, property="id") 
public class School { 
    private Integer id; 
    private String name; 

    public School() { 
    } 

    public School(Integer id, String name) { 
     this.id = id; 
     this.name = name; 
    } 

    public Integer getId() { 
     return id; 
    } 

    public void setId(Integer id) { 
     this.id = id; 
    } 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 
} 

@JsonIdentityInfo(scope=Student.class, generator=ObjectIdGenerators.PropertyGenerator.class, property="id") 
public class Student { 
    private Integer id; 
    private String name; 
    private School school; 

    public Student() { 
    } 

    public Student(Integer id, String name, School school) { 
     this.id = id; 
     this.name = name; 
     this.school = school; 
    } 

    public Integer getId() { 
     return id; 
    } 

    public void setId(Integer id) { 
     this.id = id; 
    } 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public School getSchool() { 
     return school; 
    } 

    public void setSchool(School school) { 
     this.school = school; 
    } 
} 

public static void main(String[] args) throws IOException { 
    School school = new School(1, "St Magdalene's"); 
    Student mary = new Student(1, "Mary", school); 
    Student bob = new Student(2, "Bob", school); 
    Student[] students = new Student[] {mary, bob}; 

    // Write out 
    String serialized = mapper.writeValueAsString(students); 
    System.out.println("Serialized: " + serialized); 
    // Read in 
    Student[] deserialized = mapper.readValue(serialized, Student[].class); 
} 
+0

没有工作。序列化它看起来像: '[{“id”:1,“name”:“Mary”,“school”:{“id”:1,“name”:“St Magdalene's”}},{“id” 2,“name”:“Bob”,“school”:1}]' 反序列化它并不链接到学校对象,而是只存储学校的id。 – Rawr

+0

对不起,我应该检查结果。没有类型信息,反序列化就是哈希集列表。我已经更新了示例以使用Student [],以便Jackson有足够的信息来构建对象。 – teppic

+0

作为替代方案,您可以让杰克逊管理ID。删除'id'属性并使用'generator = ObjectIdGenerators .IntSequenceGenerator'。 – teppic