2013-06-21 57 views
1

我得到一个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。 没有成功。

任何提示?

+0

显示你'Registration'类的代码,请。另外,不同类型的“结果”之间是否有共同的对象? – fge

+0

您可能想试试android-query(http://code.google.com/p/android-query/wiki/AsyncAPI) –

回答

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); 
+0

感谢您的快速回答!是否真的有必要创建这两个子类?结果:succes只是用来检查用户是否登录并且输入是否正确(响应是扫描QR码的结果)。在'SuccessfulOperation'中序列化“注册”给我的听起来很奇怪 – Charlie

+0

嗯,不,这不是必要的。这只是我在处理“多态JSON”时知道的最干净的解决方案;)你仍然没有显示你的'Registration'类是什么样的。 – fge

+0

对不起,我现在将其添加为编辑 – Charlie

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); 
       ... 
      } 
     } 
    } 
}); 
+0

我真的很喜欢Android开发。你是否建议在之后将JSONObject序列化为'Registration'对象? – Charlie