json多次级别的转换

@Autowired
    private TestService testService;

    @RequestMapping("/list/test")
    public R test() {
        List<TestEntity> tax =
            testService.list(new LambdaQueryWrapper<TestEntity>().eq(TestEntity::getSettingCode, "Tax"));
        String collect = tax.stream().map(TestEntity::getJsonValue).collect(Collectors.joining(","));
        String jsonValue = "[" + collect + "]";
        String filedNames = "countryCode,countryName";
        JSONArray objects = JSONUtil.parseArray(jsonValue);
        List<Map> jsonList = JSONUtil.toList(objects, Map.class);
        List<Map> resultList = new ArrayList<>();
        jsonList.forEach(map -> {
            Map resultMap = new HashMap();
            for (String filedName : filedNames.split(",")) {
                resultMap.put(filedName, map.get(filedName));
            }
            resultList.add(resultMap);
        });
        return R.ok().put("data", resultList);
    }

    @RequestMapping("/list/test2")
    public R test2() {

        String jsonValue = "{\"msg\":\"success\",\"code\":0,\"data\":[{\n" + "\"name\":\"xiaogou\",\n"
            + "\"age\":\"18\",\n" + "\"sex\":\"男\",\n" + "\"countryCode\":\"CN\",\n" + "\"countryName\":\"中国\"\n"
            + "},{\n" + "\"name\":\"小feng\",\n" + "\"age\":\"18\",\n" + "\"sex\":\"女\",\n" + "\"countryCode\":\"GB\",\n"
            + "\"countryName\":\"英国\"\n" + "}]}";
        String headKeys = "data";
        String filedNames = "name,age,sex";
        String[] headKeySplit = headKeys.split(",");
        JSONObject objects = JSONUtil.parseObj(jsonValue);
        JSONArray jsonArray = null;
        for (String headKey : headKeySplit) {
            if (headKey.equals(headKeySplit[headKeySplit.length - 1])) {
                jsonArray = JSONUtil.parseArray(objects.get(headKey));
            } else {
                objects = JSONUtil.parseObj(objects.get(headKey));
            }
        }
        List<Map> jsonList = JSONUtil.toList(jsonArray, Map.class);
        List<Map> resultList = new ArrayList<>();
        jsonList.forEach(map -> {
            Map resultMap = new HashMap();
            for (String filedName : filedNames.split(",")) {
                resultMap.put(filedName, map.get(filedName));
            }
            resultList.add(resultMap);
        });
        return R.ok().put("data", resultList);
    }

    @RequestMapping("/list/test3")
    public R test3() {

        String jsonValue = "{\"msg\":\"success\",\"code\":0,\"data\":[{\n" + "\"name\":\"xiaogou\",\n"
            + "\"age\":\"18\",\n" + "\"sex\":\"男\",\n" + "\"countryCode\":\"CN\",\n" + "\"countryName\":\"中国\",\n"
            + "\"school\":[\n" + "{\n" + "\"name\":\"渡头小学\",\n" + "\"code\":\"001\",\n" + "\"age\":12\n" + "}\n" + "\n"
            + "]\n" + "},{\n" + "\"name\":\"小feng\",\n" + "\"age\":\"18\",\n" + "\"sex\":\"女\",\n"
            + "\"countryCode\":\"GB\",\n" + "\"countryName\":\"英国\",\n" + "\"school\":[\n" + "{\n"
            + "\"name\":\"鄱阳中学\",\n" + "\"code\":\"002\",\n" + "\"age\":15\n" + "}\n" + "]\n" + "}]}";
        String headKeys = "data,school";
        String filedNames = "name,code";
        String[] headKeySplit = headKeys.split(",");
        JSONObject objects = JSONUtil.parseObj(jsonValue);
        JSONArray jsonArray = null;
        List<Map> resultList = new ArrayList<>();
        jsonArray = JSONUtil.parseArray(objects.get(headKeySplit[0]));
        jsonArray.stream().forEach(json -> {
            JSONArray jsonArray1 = JSONUtil.parseArray(JSONUtil.parseObj(json).get(headKeySplit[1]));
            List<Map> jsonList = JSONUtil.toList(jsonArray1, Map.class);
            jsonList.forEach(map -> {
                Map resultMap = new HashMap();
                for (String filedName : filedNames.split(",")) {
                    resultMap.put(filedName, map.get(filedName));
                }
                resultList.add(resultMap);
            });
        });
        return R.ok().put("data", resultList);
    }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值