实际上,我在 PHP 中使用 restfullyii 制作了一些 Web 服务。
但是我在用杰克逊反序列化我的网络服务的响应时遇到了一些麻烦。
这是一个响应示例:
{"success":true,"message":"Record(s) Found","data":{"totalCount":"1","user":{...}}}
为了反序列化这个响应,我制作了这个模型:
@JsonIgnoreProperties(ignoreUnknown = true)
public class response {
@JsonProperty("data")
private HashMap<String, Object> data;
@JsonProperty("message")
private String message;
@JsonProperty("success")
private Boolean success;
public HashMap<String, Object> getData() {
return data;
}
public void setData(HashMap<String, Object> data) {
this.data = data;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public Boolean getSuccess() {
return success;
}
public void setSuccess(Boolean success) {
this.success = success;
}
}
为了反序列化用户,我使用这些行:(rst 是反序列化响应的结果)
ObjectMapper mapper = new ObjectMapper();
try {
String rstTxt = String.valueOf(rst.getData().get("user"));
System.out.println(rstTxt);
user user = mapper.readValue(rstTxt, user.class);
} catch (JsonParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JsonMappingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
但它不起作用,因为 "rst.getData().get("user")" 在此模式中返回一个字符串: { attribute = value } 实际上,返回以下异常:
org.codehaus.jackson.JsonParseException: Unexpected character ('i' (code 105)): was expecting double-quote to start field name
Have you an idea about how I could do to deserialize user attribute ?
Thank you.