1

我从 httpresponse 得到以下 json

{
"result": "success",
"team_registration": {
    "current_status": "executed",
    "expiration_time": "2012-07-18T21:29:43Z",
    "id": 609,
    "team_id": 50,
    }
}

如何将“结果”作为字符串检索,将“team_registration”作为 POJO(在 Android 中)与 Jackson 一起检索?

目前我有这个:

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。没有成功。

有小费吗?

4

2 回答 2

1

如果你像这样修改你的类应该自动反序列化(注意!需要杰克逊 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);
于 2013-06-21T21:31:01.893 回答
1

你的方法对我来说似乎有点奇怪。你真的应该使用 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);
                ...
            }
        }
    }
});
于 2013-06-21T21:54:12.407 回答