笔记->JsonUtil工具类

JsonUtil工具类笔记

用到什么更新什么!!!

json字符串转换为对象

/**
     * json串转换未对象
     * @param json   源对象数据
     * @param model		需要转换的对象
     */
   public static <T> T toObject(Object jsonModel, Class<T> model){
        if (null == jsonModel){
            return null;
        }
        try{
            String json = JSONObject.toJSONString(jsonModel);
            return JSONObject.parseObject(json, model);
        }catch (Exception e){
            log.error("json转换异常{}", ExceptionUtil.getExceptionMessage(e));
            e.printStackTrace();
        }
        return null;
    }
调用(自己去校验啥的)
User user = new User();
VerifyCodeDto verifyCodeDto = JsonUtil.toObject(user, VerifyCodeDto.class);

pom文件
<!-- 阿里JSON解析器 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
        </dependency>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Object itemObj = itemList.stream().map(row -> { // 处理后的newRow格式{"clock":"2021-11-18 08:48:44","SYS-ACTIVE":30,"SYS-INACTIVE":5,"TEST-ACTIVE":4,"TEST-INACTIVE":5} JSONObject newRow = JSONUtil.createObj(); JSONObject rowJson = JSONUtil.parseObj(row); String clock = rowJson.get("clock", String.class); newRow.set("clock", clock); // [{\"MSG\":\"schema:SYS,status:ACTIVE,count:30\"},{\"MSG\":\"schema:SQLAUDIT_MON,status:INACTIVE,count:1\"},{\"MSG\":\"schema:TEST,status:ACTIVE,count:3\"},{\"MSG\":\"schema:TEST,status:INACTIVE,count:1\"}] JSONArray value = rowJson.get("value", JSONArray.class); // newValue格式示例:[{"schema":"SYS-ACTIVE","count":32},{"schema":"SYS-INACTIVE","count":3},{"schema":"TEST-INACTIVE","count":1},{"schema":"TEST-ACTIVE","count":39}] // JSONArray newValue = JSONUtil.createArray(); value.forEach(val -> { String msg = JSONUtil.parseObj(val).get("MSG", String.class); String resultExtractMulti = ReUtil.extractMulti("schema:(\\w+),status:(\\w+),count:([0-9]+)", msg, "$1-$2-$3"); if (StrUtil.isNotEmpty(resultExtractMulti)) { List<String> split = StrSplitter.split(resultExtractMulti, '-', 0, true, true); if (split.size() == 3) { String schema = split.get(0) + "-" + split.get(1); Integer count = Integer.parseInt(split.get(2)); newRow.set(schema, count); } else { log.error("MSG:{}", msg); } } }); // 汇总schema相同的用户数 // Map<String, Integer> newValueMap = newValue.stream().collect(Collectors.groupingBy(v -> JSONUtil.parseObj(v).get("schema", String.class), Collectors.summingInt(v -> JSONUtil.parseObj(v).get("count", Integer.class)))); return newRow; }).collect(Collectors.toList());这段代码可以优化吗?
06-09

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值