java对象与map对象相互转换

  1 /** 
  2  * 使用org.apache.commons.beanutils进行转换 
  3  */  
  4 class A {  
  5       
  6     public static Object mapToObject(Map<String, Object> map, Class<?> beanClass) throws Exception {    
  7         if (map == null)  
  8             return null;  
  9   
 10         Object obj = beanClass.newInstance();  
 11   
 12         org.apache.commons.beanutils.BeanUtils.populate(obj, map);  
 13   
 14         return obj;  
 15     }    
 16       
 17     public static Map<?, ?> objectToMap(Object obj) {  
 18         if(obj == null)  
 19             return null;   
 20   
 21         return new org.apache.commons.beanutils.BeanMap(obj);  
 22     }    
 23       
 24 }  
 25   
 26 /** 
 27  * 使用Introspector进行转换 
 28  */  
 29 class B {  
 30   
 31     public static Object mapToObject(Map<String, Object> map, Class<?> beanClass) throws Exception {    
 32         if (map == null)   
 33             return null;    
 34   
 35         Object obj = beanClass.newInstance();  
 36   
 37         BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());    
 38         PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();    
 39         for (PropertyDescriptor property : propertyDescriptors) {  
 40             Method setter = property.getWriteMethod();    
 41             if (setter != null) {  
 42                 setter.invoke(obj, map.get(property.getName()));   
 43             }  
 44         }  
 45   
 46         return obj;  
 47     }    
 48       
 49     public static Map<String, Object> objectToMap(Object obj) throws Exception {    
 50         if(obj == null)  
 51             return null;      
 52   
 53         Map<String, Object> map = new HashMap<String, Object>();   
 54   
 55         BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());    
 56         PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();    
 57         for (PropertyDescriptor property : propertyDescriptors) {    
 58             String key = property.getName();    
 59             if (key.compareToIgnoreCase("class") == 0) {   
 60                 continue;  
 61             }  
 62             Method getter = property.getReadMethod();  
 63             Object value = getter!=null ? getter.invoke(obj) : null;  
 64             map.put(key, value);  
 65         }    
 66   
 67         return map;  
 68     }    
 69       
 70 }  
 71   
 72 /** 
 73  * 使用reflect进行转换 
 74  */  
 75 class C {  
 76   
 77     public static Object mapToObject(Map<String, Object> map, Class<?> beanClass) throws Exception {    
 78         if (map == null)  
 79             return null;    
 80   
 81         Object obj = beanClass.newInstance();  
 82   
 83         Field[] fields = obj.getClass().getDeclaredFields();   
 84         for (Field field : fields) {    
 85             int mod = field.getModifiers();    
 86             if(Modifier.isStatic(mod) || Modifier.isFinal(mod)){    
 87                 continue;    
 88             }    
 89   
 90             field.setAccessible(true);    
 91             field.set(obj, map.get(field.getName()));   
 92         }   
 93   
 94         return obj;    
 95     }    
 96   
 97     public static Map<String, Object> objectToMap(Object obj) throws Exception {    
 98         if(obj == null){    
 99             return null;    
100         }   
101   
102         Map<String, Object> map = new HashMap<String, Object>();    
103   
104         Field[] declaredFields = obj.getClass().getDeclaredFields();    
105         for (Field field : declaredFields) {    
106             field.setAccessible(true);  
107             map.put(field.getName(), field.get(obj));  
108         }    
109   
110         return map;  
111     }   
112 }  

 

转载于:https://www.cnblogs.com/XuYiHe/p/6871799.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值