
错误示范
HashMap parseMap = JSON.parseObject(json, HashMap.class);
List<Student> studentList1 = (List<Student>) parseMap.get("studentList");
for(Student student : studentList1){ // Exception
System.out.println(student.getId() + " ");
}
代码执行会报FastJson com.alibaba.fastjson.JSONObject cannot be cast to XX.java
解决办法
使用fastjson包中的JSONArray去转换list
Stirng str= HttpUtil.get("数据接口地址");
JSONObject object = JSON.parseObject(str);
List<Student> volist = JSON.parseArray(object.getString("studentList"),Student.class);
volist.forEach(System.out::println);
报错原因:
JSONObject直接转List对象会报错。
1.套一个JSON.parseArray() 就解决了。

2.强转
ArrayList<Student> userList = JSON.parseObject(userJson, new TypeReference<ArrayList<Student>>(){});