这是我的JSON数组:-
[ { "firstName" : "abc", "lastName" : "xyz" }, { "firstName" : "pqr", "lastName" : "str" } ]
我的String对象中有这个。现在,我想将其转换为Java对象并将其存储在Java对象列表中。例如在学生对象中。我正在使用下面的代码将其转换为Java对象列表:-
ObjectMapper mapper = new ObjectMapper(); StudentList studentList = mapper.readValue(jsonString, StudentList.class);
我的清单课程是:-
public class StudentList { private List<Student> participantList = new ArrayList<Student>(); //getters and setters }
我的学生对象是:-
class Student { String firstName; String lastName; //getters and setters }
我在这里想念什么吗?我收到以下异常消息:-
Exception : com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of com.aa.Student out of START_ARRAY token
您要Jackson解析一个StudentList。告诉它解析一个List(学生)。由于List是通用的,因此通常会使用TypeReference
StudentList
List
List<Student> participantJsonList = mapper.readValue(jsonString, new TypeReference<List<Student>>(){});