net.sf.ezmorph.bean.MorphDynaBean cannot be cast to java.util.Map
Posted on 2017-03-29 14:01 和风细雨汪汪 阅读(6091) 评论(0) 编辑 收藏 举报public static void main(String[] arg0) { String reString = "{" + "\"code\": 100, \"msg\": \"\", \"data\": { \"rows\": [{ \"item\": \"小型车辆\", \"value\": \"100\" },{ \"item\": \"大型车辆\", \"value\": \"200\" }] }" + "}"; JSONObject obj = JSONObject.fromObject(reString); JSONArray jsonArray = obj.getJSONObject("data").getJSONArray("rows"); System.out.println(jsonArray.toString()); /** * 错误例子: * List<Map<String, String>> mapListJson = JSONArray.toList(jsonArray); * 会报标题上的错 */ List<Map<String, Object>> mapListJson = (List<Map<String, Object>>) JSONArray.toList(jsonArray, Map.class); Integer value = 0; for (Map<String, Object> m : mapListJson) { value += Integer.parseInt(m.get("value") == null ? "0" : m.get("value").toString()); } System.out.println(value); }
善于将复杂问题简单化