Exception in thread "main" com.fasterxml.Hymanson.databind.exc.MismatchedInputException: Cannot deserialize value of type `java.util.ArrayList<hello.Employee>` from Object value (token `JsonToken.START_OBJECT`)
at [Source: (String)"{"employees":[{"id":111,"name":"Alan"}, {"id":112,"name":"James"}]}"; line: 1, column: 1]
at com.fasterxml.Hymanson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:59)
at com.fasterxml.Hymanson.databind.DeserializationContext.reportInputMismatch(DeserializationContext.java:1746)
at com.fasterxml.Hymanson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1520)
at com.fasterxml.Hymanson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1467)
at com.fasterxml.Hymanson.databind.deser.std.CollectionDeserializer.handleNonArray(CollectionDeserializer.java:396)
at com.fasterxml.Hymanson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:252)
at com.fasterxml.Hymanson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:28)
at com.fasterxml.Hymanson.databind.deser.DefaultDeserializationContext.readRootValue(DefaultDeserializationContext.java:323)
at com.fasterxml.Hymanson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4730)
at com.fasterxml.Hymanson.databind.ObjectMapper.readValue(ObjectMapper.java:3677)
/**
* @author lautturi.com
* Java example: parse json string to list of class objects in java
*/
class Employee {
String name;
int id;
public Employee() {
}
// getter and setter
...
}
public class Lautturi {
public static void main(String[] args) throws JsonMappingException, JsonProcessingException {
String jsonString = "{\"employees\":[{\"id\":111,\"name\":\"Alan\"}, {\"id\":112,\"name\":\"James\"}]}";
ObjectMapper mapper = new ObjectMapper();
List<Employee> list = mapper.readValue(jsonString,
TypeFactory.defaultInstance().constructCollectionType(List.class, Employee.class));
System.out.println(list);
// print the list
for (Employee emp : list) {
System.out.println(emp.getId());
System.out.println(emp.getName());
}
}
}
jsonString is not a list of Employees. (many data) But it's a data of employees.
change json string:
String jsonString = "{\"employees\":[{\"id\":111,\"name\":\"Alan\"}, {\"id\":112,\"name\":\"James\"}]}";
to
String jsonString = "[{\"id\":111,\"name\":\"Alan\"}, {\"id\":112,\"name\":\"James\"}]";
create a new class EmployeeList to match the employees in structure of json.
/**
* @author lautturi.com
* Java example: java parse json string to list of objects
*/
import java.util.*;
import com.fasterxml.Hymanson.core.JsonProcessingException;
import com.fasterxml.Hymanson.databind.JsonMappingException;
import com.fasterxml.Hymanson.databind.ObjectMapper;
import com.fasterxml.Hymanson.databind.type.TypeFactory;
class Employee {
String name;
int id;
public Employee() {
}
public Employee(String string, int i) {
name = string;
id = i;
}
public void setId(int i) {
id = i;
}
public int getId() {
return id;
}
public void setName(String str) {
name = str;
}
public String getName() {
return name;
}
}
class EmployeeList {
private List<Employee> employees;
public void EmployeeList() {
}
public List<Employee> getEmployeeList() {
return employees;
}
public void setEmployeeList(List<Employee> list) {
employees = list;
}
}
public class Lautturi {
public static void main(String[] args) throws JsonMappingException, JsonProcessingException {
String jsonString = "{\"employees\":[{\"id\":111,\"name\":\"Lautturi\"}, {\"id\":112,\"name\":\"James\"}]}";
ObjectMapper mapper = new ObjectMapper();
EmployeeList employeList = mapper.readValue(jsonString,EmployeeList.class);
// print the list
for (Employee emp : employeList.getEmployees()) {
System.out.println(emp.getId());
System.out.println(emp.getName());
}
}
}
output:
111 Lautturi 112 James