我得到一个HttpResponse对象反序列化JSON与不同的对象类型
{
"result": "success",
"team_registration": {
"current_status": "executed",
"expiration_time": "2012-07-18T21:29:43Z",
"id": 609,
"team_id": 50,
}
}
如何retreive的“结果”作为字符串和“team_registration”作为一个POJO(在Android设备)与杰克逊以下JSON?
目前我有这样的:
HttpResponse response = httpClient.execute(httpGet);
if (response.getStatusLine().getStatusCode() == 200) {
HttpEntity entity = response.getEntity();
String json = EntityUtils.toString(entity);
Map<String, Object> map = mapper.readValue(json, new TypeReference<Map<String, Object>>() {
});
result = (String) map.get("result");
resultRegistration = (Registration) map.get("team_registration");
注册类:
package be.radarwerk.app.model;
import java.io.Serializable;
import java.util.Date;
import org.codehaus.jackson.annotate.JsonIgnore;
public class Registration implements Serializable { // Todo implements parceable?
private static final long serialVersionUID = 1L;
private int id;
private String currentStatus;
private Date expirationTime;
@JsonIgnore
private Volunteer volunteer;
@JsonIgnore
private Team team;
public Registration() {
}
public Registration(int id, String currentStatus, Volunteer volunteer,
Team team) {
super();
this.id = id;
this.currentStatus = currentStatus;
this.volunteer = volunteer;
this.team = team;
}
public int getId() {
return id;
}
public String getCurrentStatus() {
return currentStatus;
}
public Volunteer getVolunteer() {
return volunteer;
}
public Team getTeam() {
return team;
}
public Date getExpirationTime() {
return expirationTime;
}
}
“结果” 的字符串工作正常,但对于 “registration_moment” 我得到这个异常: java.lang.ClassCastException :java.util.LinkedHashMap不能转换为注册
我也尝试将它转换为一个字符串以“结果”相同的方式并做mapp该字符串上的er.readValue。 没有成功。
任何提示?
显示你'Registration'类的代码,请。另外,不同类型的“结果”之间是否有共同的对象? – fge
您可能想试试android-query(http://code.google.com/p/android-query/wiki/AsyncAPI) –