第九节 使用HuTool工具实现JSON解析

亮子 2023-11-25 14:50:48 20344 0 0 0
    @ApiOperation("JSONUtil使用:JSON解析工具类")
    @GetMapping("/jsonUtil")
    public CommonResult jsonUtil(){
        PmsBrand brand = new PmsBrand();
        brand.setId(1L);
        brand.setName("小米");
        brand.setShowStatus(1);
        //对象转换为JSON字符串
        String jsonStr = JSONUtil.parse(brand).toString();
        LOGGER.info("jsonUtil parse:{}",jsonStr);
        //JSON字符串转化为对象
        PmsBrand pmsBrand = JSONUtil.toBean(jsonStr, PmsBrand.class);
        LOGGER.info("jsonUtil toBean:{}",pmsBrand);
        List<PmsBrand> brandList = new ArrayList<>();
        brandList.add(pmsBrand);
        String jsonListStr = JSONUtil.parse(brandList).toString();
        //JSON字符串转换为列表
        List<PmsBrand> pmsBrandList = JSONUtil.toList(new JSONArray(jsonListStr), PmsBrand.class);
        LOGGER.info("jsonUtil toList:{}",pmsBrandList);
        return CommonResult.success(null,"操作成功!");
    }