Java对象转Map

时间:2025-03-04 21:21:56

使用包下的()方法可以将Java对象按属性转为Map。

import ;

class CommonUtils{
	public static Map<String, Object> convertToMap(Object obj) {
			try {
				if (obj instanceof Map) {
					return (Map)obj;
				}
				Map<String, Object> returnMap = (obj);
				("class");
				return returnMap;
			} catch (IllegalAccessException e1) {
				();
			} catch (InvocationTargetException e2) {
				();
			} catch (NoSuchMethodException e3) {
				();
			}
			return new HashMap();
	}
}
@Test
public void test(){
	User user=new User();
	("aa");
	(0);
	(22);
	Map<String,Object> map=(user);
	((map));
}