[英]Deserialize json with different object types
我從httpresponse獲取以下json
{
"result": "success",
"team_registration": {
"current_status": "executed",
"expiration_time": "2012-07-18T21:29:43Z",
"id": 609,
"team_id": 50,
}
}
如何與Jackson一起將“結果”作為字符串檢索,將“ team_registration”作為POJO檢索(在Android中)?
目前我有這個:
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無法轉換為注冊
我還嘗試以與“結果”相同的方式將其強制轉換為字符串,並對該字符串執行mapper.readValue。 沒有成功
有小費嗎?
如果您像這樣修改課程,則應該自動反序列化該課程( 注意!需要Jackson 2.1+ ):
@JsonIgnoreProperties("team_id")
@JsonNamingStrategy(PropertyNamingStrategy.LowerCaseWithUnderscoresStrategy)
public class Registration implements Serializable
{
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() {
}
// other code
}
然后,要反序列化您的代碼:
Registration registration;
final JsonNode node = mapper.readTree(json);
if (node.get("result").textValue().equals("success"))
registration = mapper.readObject(node.get("team_registration").traverse(),
Registration.class);
你的方法對我來說有點奇怪。 您確實應該使用Android JSONObject類,這就是它的用途。 有了JSONObject(或JSONArray)后,如果要將元素移動到其他數據結構中,則需要對其進行迭代,但這很有可能是不必要的。
無論如何,這是一些代碼(使用android-query )將您帶到JSONObject:
String url = "whatever you want";
aq.ajax(url, JSONArray.class, new AjaxCallback<JSONArray>() {
@Override
public void callback(String url, JSONArray json, AjaxStatus status) {
if (json == null) {
Toast.makeText(context, "Failed to retrieve JSON", Toast.LENGTH_SHORT);
}
else {
try {
JSONObject general = json.getJSONObject(0);
...
}
}
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.