把对象转换成map

时间:2023-03-08 18:29:58
    public static Map toMap(Object object){
Map _result = new CaseInsensitiveMap();
if (object != null) {
if (object instanceof Map){
_result.putAll((Map)object);
}
else{
PropertyDescriptor[] _props = PropertyUtils.getPropertyDescriptors(object);
for (int i = 0, length = _props.length; i < length; i++) {
PropertyDescriptor _prop = _props[i];
try {
Object v = PropertyUtils.getProperty(object, _prop.getName());
_result.put(_prop.getName(), v);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
}
return _result;
}

对应的jar包需要网上找找