Object 转Map

it2022-05-05  138

Object 转Map

/** * Object 转Map * @param obj * @return * @throws Exception */ public Map<String,Object> objToMap(Object obj) throws Exception{ Map<String,Object> map=new HashMap<>(); BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass()); PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); for (PropertyDescriptor property : propertyDescriptors) { String key = property.getName(); if (key.compareToIgnoreCase("class") == 0) { continue; } Method getter = property.getReadMethod(); Object value = getter!=null ? getter.invoke(obj) : ""; map.put(key, value); } return map; }

最新回复(0)