无法理解异常:"Cannot deserialize instance of `java.lang.String` out of START_OBJECT token" 在 Jackson 中使用 ObjectMapper
Trouble with understanding exception: "Cannot deserialize instance of `java.lang.String` out of START_OBJECT token" using ObjectMapper in Jackson
我无法将 JSON Object
正确转换为 POJO
。我有点明白问题出在哪里,但不知道如何处理。
以下是了解问题的所有具体数据:
JSONObject
我尝试反序列化(理解值名称不是理解问题的关键):
[{"name":"Rafał","description":"Przykładowy opis profilu","location":"Lublin","interests":[{"0":"Gry komputerowe","1":"Muzyka","2":"Siłownia"}],"age":24,"rowid":2,"username":"lenivius"}]
我的POJO
class:
public class Users {
private int rowid = 0, age;
private String name, username, e_mail, password, description, location;
private List<String> interests;
public Users() {
}
public Users(int rowid, int age, String name, String username, String e_mail, String password, String description, String location, List<String> interests) {
this.setRowid(rowid);
this.setAge(age);
this.setName(name);
this.setUsername(username);
this.setE_mail(e_mail);
this.setPassword(password);
this.setDescription(description);
this.setLocation(location);
this.setInterests(interests);
}
public int getRowid() {
return rowid;
}
public int getAge() {
return age;
}
public String getName() {
return name;
}
public String getUsername() {
return username;
}
public String getE_mail() {
return e_mail;
}
public String getPassword() {
return password;
}
public String getDescription() {
return description;
}
public String getLocation() {
return location;
}
public void setRowid(int rowid) {
this.rowid = rowid;
}
public void setAge(int age) {
this.age = age;
}
public void setName(String name) {
this.name = name;
}
public void setUsername(String username) {
this.username = username;
}
public void setE_mail(String e_mail) {
this.e_mail = e_mail;
}
public void setPassword(String password) {
this.password = password;
}
public void setDescription(String description) {
this.description = description;
}
public void setLocation(String location) {
this.location = location;
}
public List<String> getInterests() {
return interests;
}
public void setInterests(List<String> interests) {
this.interests = interests;
}
}
最后导致异常发生的代码行:
resultUsers = objectMapper.readValue(responseString, Users[].class);
我也可以post完整的异常消息:
com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of java.lang.String
out of START_OBJECT token
at [Source: (String)"[{"name":"Rafał","description":"Przykładowy opis profilu","location":"Lublin","interests":[{"0":"Gry komputerowe","1":"Muzyka","2":"Siłownia"}],"age":24,"rowid":2,"username":"lenivius"}]"; line: 1, column: 92] (through reference chain: java.lang.Object[][0]->com.example.loveterests.Users["interests"]->java.util.ArrayList[0])
如果我对问题的理解正确,那么 Jackson
需要 String
个对象的列表才能正确地将 JSON
转换为 POJO
,但是在“interests”键中是 JSON Array
,这可能就是问题所在。
interests
属性 有问题。在 POJO
中它由 List<String>
表示,在 JSON Payload
中由 JSON Array[JSON Object]
表示 - 包含对象的数组,而不是 strings
。您可以使用 Map<String, Object>
类型来处理:
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
class Users {
private int rowid = 0, age;
private String name, username, e_mail, password, description, location;
private List<Map<String, Object>> interests;
}
我无法将 JSON Object
正确转换为 POJO
。我有点明白问题出在哪里,但不知道如何处理。
以下是了解问题的所有具体数据:
JSONObject
我尝试反序列化(理解值名称不是理解问题的关键):
[{"name":"Rafał","description":"Przykładowy opis profilu","location":"Lublin","interests":[{"0":"Gry komputerowe","1":"Muzyka","2":"Siłownia"}],"age":24,"rowid":2,"username":"lenivius"}]
我的POJO
class:
public class Users {
private int rowid = 0, age;
private String name, username, e_mail, password, description, location;
private List<String> interests;
public Users() {
}
public Users(int rowid, int age, String name, String username, String e_mail, String password, String description, String location, List<String> interests) {
this.setRowid(rowid);
this.setAge(age);
this.setName(name);
this.setUsername(username);
this.setE_mail(e_mail);
this.setPassword(password);
this.setDescription(description);
this.setLocation(location);
this.setInterests(interests);
}
public int getRowid() {
return rowid;
}
public int getAge() {
return age;
}
public String getName() {
return name;
}
public String getUsername() {
return username;
}
public String getE_mail() {
return e_mail;
}
public String getPassword() {
return password;
}
public String getDescription() {
return description;
}
public String getLocation() {
return location;
}
public void setRowid(int rowid) {
this.rowid = rowid;
}
public void setAge(int age) {
this.age = age;
}
public void setName(String name) {
this.name = name;
}
public void setUsername(String username) {
this.username = username;
}
public void setE_mail(String e_mail) {
this.e_mail = e_mail;
}
public void setPassword(String password) {
this.password = password;
}
public void setDescription(String description) {
this.description = description;
}
public void setLocation(String location) {
this.location = location;
}
public List<String> getInterests() {
return interests;
}
public void setInterests(List<String> interests) {
this.interests = interests;
}
}
最后导致异常发生的代码行:
resultUsers = objectMapper.readValue(responseString, Users[].class);
我也可以post完整的异常消息:
com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of
java.lang.String
out of START_OBJECT token at [Source: (String)"[{"name":"Rafał","description":"Przykładowy opis profilu","location":"Lublin","interests":[{"0":"Gry komputerowe","1":"Muzyka","2":"Siłownia"}],"age":24,"rowid":2,"username":"lenivius"}]"; line: 1, column: 92] (through reference chain: java.lang.Object[][0]->com.example.loveterests.Users["interests"]->java.util.ArrayList[0])
如果我对问题的理解正确,那么 Jackson
需要 String
个对象的列表才能正确地将 JSON
转换为 POJO
,但是在“interests”键中是 JSON Array
,这可能就是问题所在。
interests
属性 有问题。在 POJO
中它由 List<String>
表示,在 JSON Payload
中由 JSON Array[JSON Object]
表示 - 包含对象的数组,而不是 strings
。您可以使用 Map<String, Object>
类型来处理:
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
class Users {
private int rowid = 0, age;
private String name, username, e_mail, password, description, location;
private List<Map<String, Object>> interests;
}