这是我的 JSON 数组:-
[
{
"firstName" : "abc",
"lastName" : "xyz"
},
{
"firstName" : "pqr",
"lastName" : "str"
}
]
我的 String 对象中有这个。现在我想将它转换为Java对象并将其存储在java对象的List中。例如在学生对象中。
我正在使用下面的代码将其转换为 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
你要求杰克逊解析一个StudentList
。告诉它解析一个List
(学生)代替。自从List
是通用的,您通常会使用类型参考
List<Student> participantJsonList = mapper.readValue(jsonString, new TypeReference<List<Student>>(){});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)