java object bean 转map

 1 import java.lang.reflect.Field;
 2 /**
 3      * obj-->map
 4      * ConvertObjToMap
 5      * 2016年8月17日上午10:53:59   
 6      * @param
 7      * @return
 8      */
 9     public static Map ConvertObjToMap(Object obj) {
10         Map<String, Object> reMap = new HashMap<String, Object>();
11         if (obj == null)
12             return null;
13         Field[] fields = obj.getClass().getDeclaredFields();
14         try {
15             for (int i = 0; i < fields.length; i++) {
16                 try {
17                     Field f = obj.getClass().getDeclaredField(fields[i].getName());
18                     f.setAccessible(true);
19                     Object o = f.get(obj);
20                     reMap.put(fields[i].getName(), o);
21                 } catch (NoSuchFieldException e) {
22                     // TODO Auto-generated catch block
23                     e.printStackTrace();
24                 } catch (IllegalArgumentException e) {
25                     // TODO Auto-generated catch block
26                     e.printStackTrace();
27                 } catch (IllegalAccessException e) {
28                     // TODO Auto-generated catch block
29                     e.printStackTrace();
30                 }
31             }
32         } catch (SecurityException e) {
33             // TODO Auto-generated catch block
34             e.printStackTrace();
35         }
36         return reMap;
37     }

 

posted @ 2016-08-17 12:46  flay  阅读(524)  评论(0编辑  收藏  举报