我有:杰森:
{
"id" : "1",
"name": "John",
"id_client": "2",
...
}
我的课:
public class Person{
private int id;
private String name;
private Client client;
...
}
public class Client{
private int id;
}
我如何将id_client:“ 2”反序列化为Client对象并设置id(CLient)?
最佳答案
...
Map<String,Object> parsed = jsonParser.parse(jsonString);
Person thePerson = new Person(parsed);
...
public class Person {
...
public Person(Map<String,Object> parsed) {
id = Integer.getInteger(parsed.getString("id"));
name = parsed.getString("name");
int idClient = Integer.getInteger(parsed.getString("id_client"));
client = new Client(idClient);
}
...
}