java實體類轉換為map


import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
/**
* java實體類轉換為map
* @author vic
*
*/

public class JavaBeanUtil {

  public static Map<String,Object> convertBeanToMap(Object bean) throws IntrospectionException,IllegalAccessException, InvocationTargetException {
  Class type = bean.getClass();
  Map<String,Object> returnMap = new HashMap<String, Object>();
  BeanInfo beanInfo = Introspector.getBeanInfo(type);
  PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
  for (int i = 0; i < propertyDescriptors.length; i++) {
    PropertyDescriptor descriptor = propertyDescriptors[i];
    String propertyName = descriptor.getName();
    if (!propertyName.equals("class")) {
      Method readMethod = descriptor.getReadMethod();
      Object result = readMethod.invoke(bean, new Object[0]);
      if (result != null) {
        returnMap.put(propertyName, result);
      } else {
        returnMap.put(propertyName, "");
      }
    }
  }
    return returnMap;
  }

}

 

文末附上博主自己寫的視頻小網站,各種最新免費電影視頻資源(搜索猴) www.sousuohou.com

 

電影公眾號:https://m.veikee.cn/

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM