我有一个映射问题,它使用jackson库将Json API回调转换为自定义对象。Json DateTime对象到GregorianCalendar
我有我的用户自定义类:
public class User {
protected String id;
protected GregorianCalendar registredAt;
protected String username;
protected Team team;
public User() {
}
public User(String id, GregorianCalendar registredAt, String username, Team team) {
this.id = id;
this.registredAt = registredAt;
this.username = username;
this.team = team;
}
public User(HashMap<String, Object> attributes) {
this.id = (String) attributes.get("id");
this.registredAt = (GregorianCalendar) attributes.get("registredAt");
this.username = (String) attributes.get("username");
this.team = (Team) attributes.get("team");
}
}
这里是我的回调:
{
id: "4feab37c5600bbd56f000132",
registredAt: {
date: "2012-06-27 09:17:15",
timezone_type: 3,
timezone: "Europe/Paris"
},
username: "John Doe",
team: {
id: "4feab37c5600bbd56f000130",
indice: 1,
name: "John Doe's Team",
score: 200
}
}
然后,我尝试初始化一个用户对象:
// Code from a doInBackground() method of a custom AsyncTask.
URL completePath = new URL(apiBaseURL + "api/user/4feab37c5600bbd56f000132/show");
APIconnection = (HttpURLConnection) completePath.openConnection();
APIconnection.setDoInput(true);
APIconnection.connect();
callbackParser = jsonFactory.createJsonParser(APIconnection.getInputStream());
response = objectMapper.readValue(callbackParser, User.class);
// Catch a JsonMappingException :
Can not deserialize instance of java.util.Calendar out of START_OBJECT token at [Source: org.apache.har[email protected]4082c7a0; line: 1, column: 33] (through reference chain: classes.models.User["registredAt"])
我想问题是回调registredAt对象是一个json_encoded PHP \ DateTime实例... 应该我更改注册的Java用户对象的类型? 或者我的构造函数中缺少一些东西?
贵公历类从日历扩展?它是否实现了Serializable接口? – 2012-07-10 11:34:38
这是一个扩展java.util.Calendar中,其中实现Serializable inteface :) 的问题是一样的,当我更换的GregorianCalendar由Calendar类(java.util中的命名空间) 不能反序列化java.util.GregorianCalendar中的类java.util.Calendar的实例超出了START_OBJECT标记。 – 2012-07-10 12:12:48
可串行化是无关紧要的 - 它不会被JSON序列化器使用,因为它只意味着JDK可以生成二进制表示。 – StaxMan 2012-07-10 19:46:25