不使用其他工具包,将java类对象转换成map类型,使用反射

   
Object map = xxx;

Map<String, Object> result = new HashMap<>();
        Class<?> clazz = map.getClass();
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            field.setAccessible(true);
            try {
                result.put(field.getName(), field.get(map));
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
       }

 

posted @ 2024-08-23 13:57  教练我想打篮球  阅读(9)  评论(0)    收藏  举报