无法从 [Source: (String)][UsersPajo{website='hildegard.org' 处的 START_ARRAY 令牌中反序列化 `model.UsersPajo` 的实例
Cannot deserialize instance of `model.UsersPajo` out of START_ARRAY token at [Source: (String)"[UsersPajo{website='hildegard.org'
我不明白为什么反序列化不起作用。
public static String readUserFile(String titleFile) {
String pathJSONFile = "src/main/resources/" + titleFile + ".json";
ObjectMapper objectMapper = new ObjectMapper();
File jsonFile = new File(pathJSONFile);
UsersPajo[] usersPajos2= null;
try {
usersPajos2 = objectMapper.readValue(jsonFile, UsersPajo[].class);
System.out.println("_usersPajos2___" + usersPajos2);
} catch (JsonProcessingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
String information = Arrays.toString(usersPajos2);
try {
usersPajo = objectMapper.readValue(information, UsersPajo.class);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
}
我已经尝试了各种选择,但看起来我 confused.Here 是个错误。
com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of
`model.UsersPajo` out of START_ARRAY token
at [Source: (String)"[UsersPajo{website='hildegard.org', address=Address{zipcode='92998-
3874', geo=Geo{lng='81.1496', lat='-37.3159'}, suite='Apt. 556', city='Gwenborough',
street='Kulas Light'}, phone='1-770-736-8031 x56442', name='Leanne Graham',
company=Company{bs='harness real-time e-markets', catchPhrase='Multi-layered client-server
neural-net', name='Romaguera-Crona'}, id=1, email='Sincere@april.biz', username='Bret'},
UsersPajo{website='anastasia.net', address=Address{zipcode='90566-7771',
geo=Geo{lng='-34.46"[truncated 3643 chars]; line: 1, column: 1]
at
这里是class模型
public class UsersPajo{
private String website;
private Address address;
private String phone;
private String name;
private Company company;
private int id;
private String email;
private String username;
public String getWebsite(){
return website;
}
public Address getAddress(){
return address;
}
public String getPhone(){
return phone;
}
public String getName(){
return name;
}
public Company getCompany(){
return company;
}
public int getId(){
return id;
}
public String getEmail(){
return email;
}
public String getUsername(){
return username;
}
@Override
public String toString() {
return "UsersPajo{" +
"website='" + website + '\'' +
", address=" + address +
", phone='" + phone + '\'' +
", name='" + name + '\'' +
", company=" + company +
", id=" + id +
", email='" + email + '\'' +
", username='" + username + '\'' +
'}';
}
}
我的JSON文件
[ {
"id" : 1,
"name" : "Leanne Graham",
"username" : "Bret",
"email" : "Sincere@april.biz",
"address" : {
"street" : "Kulas Light",
"suite" : "Apt. 556",
"city" : "Gwenborough",
"zipcode" : "92998-3874",
"geo" : {
"lat" : "-37.3159",
"lng" : "81.1496"
}
},
"phone" : "1-770-736-8031 x56442",
"website" : "hildegard.org",
"company" : {
"name" : "Romaguera-Crona",
"catchPhrase" : "Multi-layered client-server neural-net",
"bs" : "harness real-time e-markets"
}
}, ...
]
我从文件中读取信息,然后将其写入字符串。之后,我想使用反序列化从此行中获取我的 java 对象。告诉我为什么反序列化不起作用?需要修复什么
问题出在以下代码行:
try {
usersPajo = objectMapper.readValue(information, UsersPajo.class);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
变量 information
包含以下内容:
[UsersPajo{website='hildegard.org', address=Address{zipcode='92998-
3874', geo=Geo{lng='81.1496', lat='-37.3159'}, suite='Apt. 556', city='Gwenborough',
street='Kulas Light'}, phone='1-770-736-8031 x56442', name='Leanne Graham',
company=Company{bs='harness real-time e-markets', catchPhrase='Multi-layered client-server
neural-net', name='Romaguera-Crona'}, id=1, email='Sincere@april.biz', username='Bret'},
UsersPajo{website='anastasia.net', address=Address{zipcode='90566-7771',
geo=Geo{lng='-34.46"....
这基本上是 usersPajos2
映射到一个字符串。这显然不能反序列化为单个 UsersPajo
实例。所以摆脱这些代码行并保留所有其他代码:
public static String readUserFile(String titleFile) {
String pathJSONFile = "src/main/resources/" + titleFile + ".json";
ObjectMapper objectMapper = new ObjectMapper();
File jsonFile = new File(pathJSONFile);
UsersPajo[] usersPajos2= null;
try {
usersPajos2 = objectMapper.readValue(jsonFile, UsersPajo[].class);
System.out.println("_usersPajos2___" + usersPajos2);
} catch (JsonProcessingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return Arrays.toString(usersPajos2);
}
我不明白为什么反序列化不起作用。
public static String readUserFile(String titleFile) {
String pathJSONFile = "src/main/resources/" + titleFile + ".json";
ObjectMapper objectMapper = new ObjectMapper();
File jsonFile = new File(pathJSONFile);
UsersPajo[] usersPajos2= null;
try {
usersPajos2 = objectMapper.readValue(jsonFile, UsersPajo[].class);
System.out.println("_usersPajos2___" + usersPajos2);
} catch (JsonProcessingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
String information = Arrays.toString(usersPajos2);
try {
usersPajo = objectMapper.readValue(information, UsersPajo.class);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
}
我已经尝试了各种选择,但看起来我 confused.Here 是个错误。
com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of
`model.UsersPajo` out of START_ARRAY token
at [Source: (String)"[UsersPajo{website='hildegard.org', address=Address{zipcode='92998-
3874', geo=Geo{lng='81.1496', lat='-37.3159'}, suite='Apt. 556', city='Gwenborough',
street='Kulas Light'}, phone='1-770-736-8031 x56442', name='Leanne Graham',
company=Company{bs='harness real-time e-markets', catchPhrase='Multi-layered client-server
neural-net', name='Romaguera-Crona'}, id=1, email='Sincere@april.biz', username='Bret'},
UsersPajo{website='anastasia.net', address=Address{zipcode='90566-7771',
geo=Geo{lng='-34.46"[truncated 3643 chars]; line: 1, column: 1]
at
这里是class模型
public class UsersPajo{
private String website;
private Address address;
private String phone;
private String name;
private Company company;
private int id;
private String email;
private String username;
public String getWebsite(){
return website;
}
public Address getAddress(){
return address;
}
public String getPhone(){
return phone;
}
public String getName(){
return name;
}
public Company getCompany(){
return company;
}
public int getId(){
return id;
}
public String getEmail(){
return email;
}
public String getUsername(){
return username;
}
@Override
public String toString() {
return "UsersPajo{" +
"website='" + website + '\'' +
", address=" + address +
", phone='" + phone + '\'' +
", name='" + name + '\'' +
", company=" + company +
", id=" + id +
", email='" + email + '\'' +
", username='" + username + '\'' +
'}';
}
}
我的JSON文件
[ {
"id" : 1,
"name" : "Leanne Graham",
"username" : "Bret",
"email" : "Sincere@april.biz",
"address" : {
"street" : "Kulas Light",
"suite" : "Apt. 556",
"city" : "Gwenborough",
"zipcode" : "92998-3874",
"geo" : {
"lat" : "-37.3159",
"lng" : "81.1496"
}
},
"phone" : "1-770-736-8031 x56442",
"website" : "hildegard.org",
"company" : {
"name" : "Romaguera-Crona",
"catchPhrase" : "Multi-layered client-server neural-net",
"bs" : "harness real-time e-markets"
}
}, ...
]
我从文件中读取信息,然后将其写入字符串。之后,我想使用反序列化从此行中获取我的 java 对象。告诉我为什么反序列化不起作用?需要修复什么
问题出在以下代码行:
try {
usersPajo = objectMapper.readValue(information, UsersPajo.class);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
变量 information
包含以下内容:
[UsersPajo{website='hildegard.org', address=Address{zipcode='92998-
3874', geo=Geo{lng='81.1496', lat='-37.3159'}, suite='Apt. 556', city='Gwenborough',
street='Kulas Light'}, phone='1-770-736-8031 x56442', name='Leanne Graham',
company=Company{bs='harness real-time e-markets', catchPhrase='Multi-layered client-server
neural-net', name='Romaguera-Crona'}, id=1, email='Sincere@april.biz', username='Bret'},
UsersPajo{website='anastasia.net', address=Address{zipcode='90566-7771',
geo=Geo{lng='-34.46"....
这基本上是 usersPajos2
映射到一个字符串。这显然不能反序列化为单个 UsersPajo
实例。所以摆脱这些代码行并保留所有其他代码:
public static String readUserFile(String titleFile) {
String pathJSONFile = "src/main/resources/" + titleFile + ".json";
ObjectMapper objectMapper = new ObjectMapper();
File jsonFile = new File(pathJSONFile);
UsersPajo[] usersPajos2= null;
try {
usersPajos2 = objectMapper.readValue(jsonFile, UsersPajo[].class);
System.out.println("_usersPajos2___" + usersPajos2);
} catch (JsonProcessingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return Arrays.toString(usersPajos2);
}