[英]Inner static object deserialization with Jackson
I'm trying to deserialize json like this: 我试图像这样反序列化json:
{ "User.getToken": { "code": "200", "token": "0155649ce3ea4be98da9fc29376d8688", "user_name": "Andruw137", "user_avatar": "https://text.com/images/avatar/_/81546.png" } } {“ User.getToken”:{“代码”:“ 200”,“令牌”:“ 0155649ce3ea4be98da9fc29376d8688”,“ user_name”:“ Andruw137”,“ user_avatar”:“ https://text.com/images/avatar/_ /81546.png“}}
with these lines of code: 这些代码行:
ObjectMapper mapper = new ObjectMapper();
TokenRequest tokenRequest = mapper.readValue(String.valueOf(response), TokenRequest.class);
and this: 和这个:
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;
}
}
} }
I've read through documentation but still getting error: 我已经阅读了文档,但仍然出现错误:
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]
I'd like to understand what that error message means and how to actually get such a json deserialized. 我想了解该错误消息的含义以及如何真正反序列化这样的json。 Thanks in advance for any help. 在此先感谢您的帮助。
You are trying to deserialize a string "code": "200"
into an integer. 您正在尝试将字符串"code": "200"
反序列化为整数。 This will work if you remove the quotes: "code": 200
. 如果您删除引号: "code": 200
这将起作用。
Also, please add @JsonProperty("json_name") annotation to the object fields and name them using standard camel case: 另外,请在对象字段中添加@JsonProperty(“ json_name”)批注,并使用标准驼峰式大小写来命名它们:
@JsonProperty("user_avatar") String userAvatar;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.