把对象转换成map


public static Map transBean2Map(Object obj)
    {
        if(obj == null)
            return null;
        Map map = new HashMap();
        try
        {
            BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
            PropertyDescriptor propertyDescriptors[] = beanInfo.getPropertyDescriptors();
            PropertyDescriptor apropertydescriptor[] = propertyDescriptors;
            int i = apropertydescriptor.length;
            for(int j = 0; j < i; j++)
            {
                PropertyDescriptor property = apropertydescriptor[j];
                String key = property.getName();
                if(!key.equals("class"))
                {
                    Method getter = property.getReadMethod();
                    Object value = getter.invoke(obj, new Object[0]);
                    map.put(key, value);
                }
            }

        }
        catch(Exception e)
        {
            System.out.println((new StringBuilder()).append("transBean2Map Error ").append(e).toString());
        }
        return map;
    }

  


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM