[英]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.