遍历我只看懂了这么多,哪位大佬可以讲一下,在进行简化一下
数据字段:area_count字段类型:[{"areaNums":39,"region":"锡山区"},{"areaNums":61,"region":"惠山区"}]
//从数据库获取字段
List<Map<String, Object>> typelist = dataShareLogMapper.selectMaps(new QueryWrapper<TDataShareLog>().eq("is_delete", '0').eq("user_type", 0).select("area_count"));
创建一个Map进行存储
Map<Object, Object> map = new HashMap<>();
//遍历数据库里的这个area_count字段
for (Map<String, Object> stringObjectMap : typelist) {
//转化这个area_count字段
Object area_count = stringObjectMap.get("area_count");
//再把转换过后的area_count在进行JSON转换
JSONArray objects1 = JSONObject.parseArray(String.valueOf(area_count));
//遍历转换成JSON的objects1属性
for (int i = 0; i < objects1.size(); i++) {
//获取遍历后的值
Object o = objects1.get(i);
JSONObject jsonObject = JSONObject.parseObject(String.valueOf(o));
//把取到的key值塞到map中
map.put(jsonObject.getString("region"),jsonObject.getInteger("areaNums"));
for (int j = 0; j < objects1.size(); j++) {
if (j!=i){
Object o1 = objects1.get(j);
JSONObject jsonObject1 = JSONObject.parseObject(String.valueOf(o));
if(jsonObject.getString("region").equals(jsonObject1.getString("region"))){
if (map.containsKey(jsonObject.getString("region"))){
Object region = map.get(jsonObject.getString("region"));
Integer integer = Integer.valueOf(region.toString());
Integer areaNums = jsonObject.getInteger("areaNums");
map.put(jsonObject.getString("region"),integer+areaNums);
}
}
}
}
}
//输出map
System.out.println(map);
}