[英]Exception in thread “main” java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to Custom Object
I have a JSON as below我有一个 JSON 如下
[{
"empId": 22,
"Active": true,
"dept": {
"deptID": 507,
"deptDetails": [{
"deptName": "CSE",
"CreateDate": "2010-11-15T15:27:45.263"
}]
}
}]
I am trying to read the deptName as shown below我正在尝试读取 deptName ,如下所示
public class TestEmp {
public static void main(String[] args) throws IOException {
String json = "[{\r\n" +
" \"empId\": 22,\r\n" +
" \"Active\": true,\r\n" +
" \"dept\": {\r\n" +
" \"deptID\": 507,\r\n" +
" \"deptDetails\": [{\r\n" +
" \"deptName\": \"CSE\",\r\n" +
" \"CreateDate\": \"2010-11-15T15:27:45.263\"\r\n" +
" }]\r\n" +
" }\r\n" +
"}]";
List<Map<String, Object>> lstMpAffOffers = convertJsonToListMap(json);
for (Map<String, Object> map : lstMpAffOffers) {
Map<String, Object> deptMap = (Map<String, Object>) map.get("dept");
List<DeptDetail> deptDetail = (List<DeptDetail>) deptMap.get("deptDetails");
System.out.println(deptDetail.get(0).getDeptName());
}
}
public static List<Map<String, Object>> convertJsonToListMap(String json) throws IOException {
ObjectMapper objectMapper = new ObjectMapper();
return objectMapper.readValue(json, new TypeReference<List<Map<String, Object>>>() {
});
}
}
DeptDetail.java DeptDetail.java
public class DeptDetail {
private String deptName;
public String getDeptName() {
return deptName;
}
public void setDeptName(String deptName) {
this.deptName = deptName;
}
public String getCreateDate() {
return CreateDate;
}
public void setCreateDate(String createDate) {
CreateDate = createDate;
}
private String CreateDate;
}
When i try to read the deptName i am getting the following error当我尝试读取 deptName 时,出现以下错误
Exception in thread "main" java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to com.DeptDetail线程“主”java.lang.ClassCastException 中的异常:java.util.LinkedHashMap 无法转换为 com.DeptDetail
First, cast it into object array then cast into DeptDetail
for each element首先,将其转换为 object 数组,然后为每个元素转换为DeptDetail
Object[] objects=(Object[])deptMap.get("deptDetails");
List<DeptDetail> deptDetails = new ArrayList<DeptDetail>();
for (Object obj : objects) {
DeptDetail deptDetail = (DeptDetail)obj;
deptDetails.add(deptDetail);
}
You need to explicitly convert the "deptDetails" object to a list of your DeptDetail class您需要将“deptDetails”object 显式转换为您的 DeptDetail class 列表
public class TestEmp {
public static void main(String[] args) throws IOException {
String json = "[{\r\n" +
" \"empId\": 22,\r\n" +
" \"Active\": true,\r\n" +
" \"dept\": {\r\n" +
" \"deptID\": 507,\r\n" +
" \"deptDetails\": [{\r\n" +
" \"deptName\": \"CSE\",\r\n" +
" \"CreateDate\": \"2010-11-15T15:27:45.263\"\r\n" +
" }]\r\n" +
" }\r\n" +
"}]";
List<Map<String, Object>> lstMpAffOffers = convertJsonToListMap(json);
ObjectMapper objectMapper = new ObjectMapper();
for (Map<String, Object> map : lstMpAffOffers) {
Map<String, Object> deptMap = (Map<String, Object>) map.get("dept");
List<DeptDetail> deptDetail = objectMapper.convertValue(deptMap.get("deptDetails"), new TypeReference<List<DeptDetail>>(){})
System.out.println(deptDetail.get(0).getDeptName());
}
}
public static List<Map<String, Object>> convertJsonToListMap(String json) throws IOException {
ObjectMapper objectMapper = new ObjectMapper();
return objectMapper.readValue(json, new TypeReference<List<Map<String, Object>>>() {
});
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.