1.传入一个List对象,及该对象的属性名获取该List中所有该属性的值(属性类型为String)

private static List<String> getWybsListValueByName(String fieldName, List list) {
        try {
            List<String>lists=new ArrayList<>(list.size());
            for (Object object:list) {
                String firstCode = fieldName.substring(0, 1).toUpperCase();
                String getter = "get" + firstCode + fieldName.substring(1);
                Method method = object.getClass().getMethod(getter);
                Object value = method.invoke(object);
                lists.add(value.toString());
            }
            return lists;
        } catch (Exception e) {
            return null;
        }
    }

 获取某个对象全部属性及属性值映射成map

    public static Map getDataList(test test,Object o) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        Map<String,Object>map=new HashMap<>();
        Field[] declaredFields = o.getClass().getDeclaredFields();
        for (Field declaredField : declaredFields) {
            String s = declaredField.toGenericString();
            while (s.contains(".")) {
                s = s.substring(s.indexOf(".") + 1);
            }
            String name=s;
            //get方法list
            s = "get" + s.substring(0, 1).toUpperCase() + s.substring(1);
            Object value = o.getClass().getMethod(s).invoke(o);
            map.put(name,value);
        }
        System.out.println("result ======"+map);
        return map;
    }

 

相关文章:

  • 2021-05-31
  • 2021-05-29
  • 2021-09-02
  • 2021-12-13
  • 2021-12-13
  • 2021-12-10
  • 2022-01-23
猜你喜欢
  • 2021-12-22
  • 2021-06-28
  • 2022-12-23
  • 2021-10-31
  • 2021-08-09
  • 2022-12-23
  • 2021-11-18
相关资源
相似解决方案