I can’t understand why deserialization doesn’t work.
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();
}
}
I've already tried various options, but it looks like I'm confused.Here is the mistake.
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='[email protected]', username='Bret'},
UsersPajo{website='anastasia.net', address=Address{zipcode='90566-7771',
geo=Geo{lng='-34.46"[truncated 3643 chars]; line: 1, column: 1]
at
Here is the class model
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 '\''
'}';
}
}
My JSON file
[ {
"id" : 1,
"name" : "Leanne Graham",
"username" : "Bret",
"email" : "[email protected]",
"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"
}
}, ...
]
I read information from a file and then write it into a string. After that, I want to get my java object from this line using desrialization. Tell me why desirialization does not work? What needs to be fixed
CodePudding user response:
The issue is the following lines of code:
try {
usersPajo = objectMapper.readValue(information, UsersPajo.class);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
The variable information
contains the following:
[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='[email protected]', username='Bret'},
UsersPajo{website='anastasia.net', address=Address{zipcode='90566-7771',
geo=Geo{lng='-34.46"....
Which basically is the usersPajos2
mapped to a String. This obviously can't be deserialized into a single UsersPajo
instance. So get rid of these lines of code and keep all others:
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);
}