java.lang.ClassCastException: java.util.HashMap cannot be cast to com.alibaba.fastjson.JSONObject
您好: 我fastJson将版本从1.2.47升级到1.2.58。在启动项目的时候发现java.lang.ClassCastException: java.util.HashMap cannot be cast to com.alibaba.fastjson.JSONObject。debug发现是如下代码导致的 for (Object orgTypesObject : orgTypes) { JSONObject orgType1 = (JSONObject) orgTypesObject; } 但是如果我改成 for(int i=0 ; i< orgTypes.size() ; i++){ JSONObject orgType2 = orgTypes.getJSONObject(i); } 是可行,请问为什么58版本不支持强制转换了?
再次补充一下,JSONObject是请求dubbo接口返回的。如果我不请求dubbo接口而是通过HTTP或者其他方式,是可以进行强转的
再次补充一下,JSONObject是请求dubbo接口返回的。如果我不请求dubbo接口而是通过HTTP或者其他方式,是可以进行强转的
1.2.54版本对JSONObject的getJSONObject() getJSONArarry()方法进行了修改,增加了 对 list map的特殊处理
{"configInfo":[{"type":"appaMonitor","config":[{"name":"serviceCenterDaily","value":{"monitorPeriod":"1","alarmShow":{"submit":"0","submitTarget":"beforehand"},"targetIps":"127.0.0.1/2","statisticsGrain":"10","remark":"","thresholdConfig":{"rate":10,"counts":10,"oper":"and"}}}]}]} 这个json 前端jsp通过post传回来时候spring 直接转换为JSONObject 中的configInfo 节点数据 是ArrayList LinkedHashMap 但是直接用main方法直接转换为 JSONObject configInfo 节点则是JSONArray JSONObject @Bin-mario
String str = "{"configInfo":[{"type":"appaMonitor","config":[{"name":"serviceCenterDaily","value":{"monitorPeriod":"1","alarmShow":{"submit":"0","submitTarget":"beforehand"},"targetIps":"127.0.0.1/2","statisticsGrain":"10","remark":"","thresholdConfig":{"rate":10,"counts":10,"oper":"and"}}}]}]}"; JSONObject obj = JSON.parseObject(str);
如何设置才能使前端jsp页面post过来的数据格式和java代码自己转成JSONObje的数据结构是一致的?希望还是原来的JSONArray 而不是 ArrayList
@wenshao 请问为什么从网络拿到的JSONObject里面存的是ArrayList和LinkedHashMap不能进行强转,但是我用String接收本地转换内部是JSONObject和JSONArray,请问为什么同样的数据在不同的场景内得到的数据结构完全不一样呢?
同样遇到了这个问题
我发现,CodeExtractor.Tree里的内容拿不出来了?
HashMap<String, HashMap<String, HashMap<Integer, CodeExtractor.Tree>>> tree
= JSON.parseObject(line, new TypeReference<HashMap<String, HashMap<String, HashMap<Integer, CodeExtractor.Tree>>>>() {});
我发现,CodeExtractor.Tree里的内容拿不出来了?
HashMap<String, HashMap<String, HashMap<Integer, CodeExtractor.Tree>>> tree = JSON.parseObject(line, new TypeReference<HashMap<String, HashMap<String, HashMap<Integer, CodeExtractor.Tree>>>>() {});
问题解决了。需要设置一个构造函数,每个变量都需要是参数。