标题:遍历我只看懂了这么多,哪位大佬可以讲一下,在进行简化一下
只看楼主
刘腾龙
Rank: 2
来 自:河南省
等 级:论坛游民
威 望:1
帖 子:32
专家分:20
注 册:2021-4-12
 问题点数:0 回复次数:0 
遍历我只看懂了这么多,哪位大佬可以讲一下,在进行简化一下
数据字段: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);
        }
搜索更多相关主题的帖子: Object 字段 Map 遍历 Integer 
2021-07-16 14:18



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-506252-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.082437 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved