我得到一個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) –