• Java 获取类中所有字段 转换为 json字符串 Java 类中字段转换为json字符串 javabean 字段 转换为 jsonStr


    Java 获取类中所有字段 转换为 json字符串 Java 类中字段转换为json字符串 javabean 字段 转换为 jsonStr

    一、概述

            最近开发工作中,需要将类中的所有字段获取出来,并转换为json字符串,予以存储起来,用于poi excel导出,可以根据实际需要,增、减字段,来调整导出列的需求。 本文将 使用 反射的方式,获取类中的字段,存储到map中,再使用Fastjson转换为json字符串,用于输出存储。

    二、代码实现

            1、依赖 pom

    1. <dependency>
    2. <groupId>com.alibabagroupId>
    3. <artifactId>fastjsonartifactId>
    4. <version>1.2.83version>
    5. dependency>
    6. <dependency>
    7. <groupId>io.springfoxgroupId>
    8. <artifactId>springfox-swagger2artifactId>
    9. <version>2.9.2version>
    10. dependency>
    11. <dependency>
    12. <groupId>io.springfoxgroupId>
    13. <artifactId>springfox-swagger-uiartifactId>
    14. <version>2.9.2version>
    15. dependency>

            2、代码

    1. /**
    2. * Description: 生成类的字段 映射 map
    3. * @param clz 转换类的 class 对象
    4. * @param excludeList 排除的字段名
    5. * @param clzAnon 注解名 --- 用于字段 注释信息获取
    6. * @return java.util.List>
    7. * @version v1.0
    8. * @author wu
    9. * @date 2022/9/26 21:04
    10. */
    11. public List> getPropertyMap(Class clz, List excludeList, Class clzAnon) {
    12. List fieldList = Arrays.stream(clz.getDeclaredFields()).collect(Collectors.toList());
    13. List> mapList = Lists.newArrayList();
    14. int n = 1;
    15. for (Field f : fieldList) {
    16. String name = f.getName();
    17. if (CollectionUtils.isNotEmpty(excludeList)) {
    18. if (excludeList.contains(name)) continue;
    19. }
    20. HashMap tempMap = Maps.newHashMap();
    21. Annotation annotation = f.getAnnotation(clzAnon);
    22. String colName = name;
    23. if (annotation instanceof ApiModelProperty) {
    24. ApiModelProperty a = (ApiModelProperty) annotation;
    25. colName = a.value();
    26. }
    27. // add more Annotation support ...
    28. tempMap.put("colIndex", "" + n ++);
    29. tempMap.put("colName", colName);
    30. tempMap.put("property", name);
    31. mapList.add(tempMap);
    32. }
    33. return mapList;
    34. }

            3、测试

    1. @Test
    2. public void genPropertyMap(){
    3. ArrayList list = Lists.newArrayList();
    4. list.add("num22"); // 过滤掉属性num22
    5. List> propertyMap = getPropertyMap(Child.class, list, ApiModelProperty.class);
    6. System.out.println("propertyMap: \n"+ JSON.toJSONString(propertyMap));
    7. }

            3.1、输出结果如下:

    1. propertyMap:
    2. [
    3. {
    4. "colIndex": "1",
    5. "colName": "id2号",
    6. "property": "id22"
    7. },
    8. {
    9. "colIndex": "2",
    10. "colName": "名字2号",
    11. "property": "name22"
    12. },
    13. {
    14. "colIndex": "3",
    15. "colName": "地址2号",
    16. "property": "addr22"
    17. }
    18. ]

            4、补充:Child 类结构如下

    1. public class Child {
    2. @ApiModelProperty(value = "id2号")
    3. public int id22 ;
    4. @ApiModelProperty(value = "数字2号")
    5. protected int num22 ;
    6. @ApiModelProperty(value = "名字2号")
    7. String name22 ;
    8. @ApiModelProperty(value = "地址2号")
    9. private String addr22;
    10. // ignore getter/setter
    11. }

    三、总结

            1、获取一个类中的所有字段,可以使用反射来实现; 转换json字符串,可以使用 Fastjson、Gson、Jackson 等工具类实现 ..

            2、关于反射,注解等相关的知识,可以参考:

            3、idea 方法注释模板设置: Idea 设置方法注释模板 Idea 2021.2配置方法类注释模板_HaHa_Sir的博客-CSDN博客_idea设置方法注释模板

  • 相关阅读:
    SQL 行列转换
    Bit.Store:熊市漫漫,稳定Staking产品或成主旋律
    Mybatis使用中的坑
    排序算法之归并排序
    C++ merge()和inplace_merge()函数用法详解(深入了解,一文学会)
    Redis:Redis的数据结构、key的操作命令
    循环神经网络(RNN)之长短期记忆(LSTM)
    07 MyBatis之高级映射 + 懒加载(延迟加载)+缓存
    输出格式说明符%u
    【Spring容器的启动过程】
  • 原文地址:https://blog.csdn.net/HaHa_Sir/article/details/127103702