我有两个文件:people.json
和[
{ "name": "director", "pay": "100000"},
{ "name": "programmer", "pay": "75000"},
{ "name": "teacher", "pay": "50000"}
]
。前者只是一系列职业:
[
{ "name": "Mary", "occupation": "programmer" },
{ "name": "Jane", "occupation": "director" },
{ "name": "John", "occupation": "teacher" }
]
后者是一些人和他们的职业:
public class Occupation {
private final String name;
private final int pay;
public String getName() { ... }
public int getPay() { ... }
}
public class Person {
private final String name;
private final Occupation occupation;
public String getName() { ... }
public String getOccupation() { ... }
}
这些是相应的类:
Person
目前我正在使用ObjectMapper.readValue(InputStream, Class)
反序列化文件。如何让Occupation
类了解所有现有的{{1}}个对象?我想用职业的名字选择一个人的职业。
答案 0 :(得分:0)
向Person.class添加一个函数
public Occupation getOccupation_()
{
// assume you had a static occupations list...
for (Occupation o : occupations)
if (o.name == this.occupation)
return o;
return null;
}