异常 java.util.LinkedHashMap 不能转换为 java.util.List
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35570974/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Exception java.util.LinkedHashMap cannot be cast to java.util.List
提问by Vamsi
While working around following code I got the exception given below.
在处理以下代码时,我得到了下面给出的异常。
List<Map<String, Object>> obj = mapper.readValue(result.getBody(), new TypeReference<Map<String,Object>>(){});
for (Map<String, Object> map : obj) {
for(Map.Entry<String, Object> entry : map.entrySet()){
System.out.println("Key : "+entry.getKey()+" Value is: "+entry.getValue());
}
}
Stacktrace: java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to java.util.List
堆栈跟踪:java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to java.util.List
回答by Benjamin
Try This : Are you trying to Store MapObject in List.so instead of Map store in List.
试试这个:您是否尝试将 MapObject 存储在 List.so 中,而不是将 Map 存储在 List 中。
instead of:
代替:
List<Map<String, Object>> obj = mapper.readValue(result.getBody(),
new TypeReference<Map<String,Object>>(){});
Use This :
用这个 :
List<Map<String, Object>> obj = mapper.readValue(result.getBody(),
new TypeReference<List<Map<String,Object>>>(){});
For Demo example This Link
对于演示示例此链接
回答by Lilia
You need to use only a Map instead of List of Map.
您只需要使用地图而不是地图列表。
Map<String, Object>> object = mapper.readValue(result.getBody(), new TypeReference<Map<String,Object>>(){});
for(Map.Entry<String, Object> entry : object.entrySet()){
System.out.println("Key : "+entry.getKey()+" Value is:"+entry.getValue());
}