最近工作上有个需求,需要将Map<String,EncryptInfoBo> 转换为json字符串之后,再将该json字符串复原为Map<String,EncryptInfoBo>,利用Hutool工具类可以实现此需求。

public static void main(String[] args) {
        Map<String, EncryptInfoBo> map = new HashMap<>();
        map.put("a", new EncryptInfoBo("1", "2", "3"));
        map.put("b", new EncryptInfoBo("1", "2", "3"));

        String s = JSONUtil.toJsonStr(map);

        System.out.println(s);

        TypeReference<Map<String, EncryptInfoBo>> reference = new TypeReference<Map<String, EncryptInfoBo>>() {
        };

        Map<String, EncryptInfoBo> infoBoMap = JSONUtil.toBean(s, reference, true);

        EncryptInfoBo encryptInfoBo = infoBoMap.get("a");
        System.out.println(JSONUtil.toJsonStr(encryptInfoBo));


    }