实体类转Map

public static Map<String, String> getData(Object obj){

Map<String, String> data = new TreeMap<String, String>();

Field[] fields = obj.getClass().getDeclaredFields();

for(Field field : fields){

field.setAccessible(true);

try {

if(!Modifier.isFinal(field.getModifiers())){

Object val = field.get(obj);

if(val != null){

data.put(field.getName(), val.toString());

}

}

} catch (IllegalArgumentException e) {

e.printStackTrace();

} catch (IllegalAccessException e) {

e.printStackTrace();

}

}

return data;

}


你可能感兴趣的:(实体类转Map)