【发布时间】:2013-10-04 21:38:50
【问题描述】:
我正在使用 Jackson 来序列化/反序列化 JSON 对象。
我有一个Study 对象的以下 JSON:
{
"studyId": 324,
"patientId": 12,
"patient": {
"name": "John",
"lastName": "Doe"
}
}
更新:很遗憾,无法修改 JSON 结构。这是问题的一部分。
我想将对象反序列化为以下类:
public class Study {
Integer studyId;
Patient patient;
}
和
public class Patient {
Integer patientId;
String name;
String lastName;
}
是否可以在Patient 对象中包含patientId 属性?
我能够将patient 对象反序列化为Patient 类(具有相应的name 和lastName 属性),但无法包含patientId 属性。
有什么想法吗?
【问题讨论】:
-
我真的不知道杰克逊是否会喜欢这个,但你可以尝试将
Patient作为Study中的内部类并将getPatientId()实现为return Study.this.patientId以使似乎数据首先应该属于它。 (不。如果我不需要,我不会这样做。)
标签: json jackson deserialization