zoukankan      html  css  js  c++  java
  • 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 }  
  • 相关阅读:
    浅析c#中new、override、virtual关键字的区别
    数据解析1113
    Silverlight中xaml之间的跳转方案之一
    silverlight读取client文件的完整路径
    (Transfered)WPF Tutorial:Beginning
    数据解析1112
    邮件发送1
    TortoiseSVN Settings Subversion
    德信无线10Q4净利润同比增进187%
    欧盟中止对我数据卡双反调查
  • 原文地址:https://www.cnblogs.com/XuYiHe/p/6871799.html
Copyright © 2011-2022 走看看