實體類物件轉map集合
阿新 • • 發佈:2019-02-19
存著備忘。。。public static Map getValue(Object thisObj) { Map map = new HashMap(); Class c; try { c = Class.forName(thisObj.getClass().getName()); Method[] m = c.getMethods(); for (int i = 0; i < m.length; i++) { String method = m[i].getName(); if (method.startsWith("get")) { try { Object value = m[i].invoke(thisObj); if (value != null && !"getClass".equals(method)) { String key = method.substring(3); key = key.substring(0, 1).toUpperCase()+ key.substring(1); map.put(method.substring(3).toLowerCase(), value); } } catch (Exception e) { System.out.println("error:" + method); } } } } catch (Exception e) { // TODO: handle exception } return map; }