我正试图像这样反序列化json:
{ “User.getToken”:{ “代码”:“200”, “token”:“0155649ce3ea4be98da9fc29376d8688”, “user_name”:“Andruw137”, “user_avatar”:“https://text.com/images/avatar/_/81546.png” } }
使用以下代码行:
ObjectMapper mapper = new ObjectMapper();
TokenRequest tokenRequest = mapper.readValue(String.valueOf(response), TokenRequest.class);
和此:
public class TokenRequest {
@JsonProperty("User.getToken")
public User user = new User();
public User getUser() {
return user;
}
public void setUser(User user) {
this.User = user;
}
static class User {
int code;
String token;
String user_name;
String user_avatar;
public int getCode() {
return code;
}
public String getToken() {
return token;
}
public String getUser_name() {
return user_name;
}
public String getUser_avatar() {
return user_avatar;
}
public void setCode(int code) {
this.code = code;
}
public void setToken(String token) {
this.token = token;
}
public void setUser_name(String user_name) {
this.user_name = user_name;
}
public void setUser_avatar(String user_avatar) {
this.user_avatar = user_avatar;
}
}
}
我已阅读文档,但仍然收到错误:
com.fasterxml.jackson.databind.JsonMappingException: Can not instantiate value of type [simple type, class ru.amdm.amdm.authorization.TokenRequest] from Integral number (404); no single-int-arg constructor/factory method
at [Source: 404 .1; line: 1, column: 1]
我想了解错误消息的含义以及如何实际获得这样的json反序列化。提前感谢您的帮助。
答案 0 :(得分:2)
您正在尝试将字符串"code": "200"
反序列化为整数。如果您删除引号,这将有效:"code": 200
。
另外,请将@JsonProperty(“json_name”)注释添加到对象字段,并使用标准的驼峰案例命名它们:
@JsonProperty("user_avatar")
String userAvatar;