Thanks for the question about the mismatch of property name. However, I have made them consistent but same error throws.
I'm using Jackson to convert JSON array to Java object array.
The code is as simple as below, here is the code entry:
import java.io.File;
import com.fasterxml.jackson.databind.ObjectMapper;
import jackson.vo.User;
public class JsonConvertTest {
public static void main(String args[]){
try{
ObjectMapper objectMapper = new ObjectMapper();
File file = new File("results.json");
User[] users= objectMapper.readValue(file, User[].class);
}catch(Exception e){
e.printStackTrace();
}
}
}
Here is the value object,
package jackson.vo;
import com.fasterxml.jackson.annotation.JsonProperty;
public class User {
@JsonProperty("firstName")
String firstName;
@JsonProperty("lastName")
String lastName;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
Here is the JSON array:
{
"users":[
{ "firstName":"Tom", "lastName":"Jackson"},
{ "firstName":"Jenny", "lastName":"Mary"},
{ "firstName":"Red", "lastName":"Blue"},
{ "firstName":"Jason", "lastName":"John"},
{ "firstName":"May", "lastName":"Black"}
]
}
The output is:
com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `jackson.vo.User[]` out of START_OBJECT token
at [Source: (File); line: 1, column: 1]
Thanks for your help in advance.