【发布时间】:2016-11-23 03:12:26
【问题描述】:
// 我使用这个简单的程序: public static Object convertToBean(Class type, Map map) { BeanInfo beanInfo; 对象 obj = null; 尝试 { beanInfo = Introspector.getBeanInfo(type); obj = type.newInstance();
// When I debugging to here, I found that some properties is different from the variable the Object own. PropertyDescriptor changes charactor case when the variable is not in "String" type.
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for (PropertyDescriptor descriptor : propertyDescriptors) {
String propertyName = descriptor.getName();
if (map.containsKey(propertyName)) {
Object value = map.get(propertyName);
Object[] args = new Object[1];
args[0] = value;
descriptor.getWriteMethod().invoke(obj, args);
}
}
} catch (Exception ignored) {
}
return obj;
}
//Using BeanMap is the same question.
【问题讨论】:
-
比如我想把Map转成一个Bean,map中的数据:A01->0.01;A02->0.02;AD->"12345678" Java Bean定义:private Double A01;私人双 A02;私人字符串 AD;设置者和获取者。这样AD就可以正确设置,但是A01和A02将为空。
标签: dictionary javabeans propertydescriptor