1
//この単純なプログラムを使用します: public static Object convertToBean(クラスタイプ、マップマップ){ BeanInfo beanInfo; オブジェクトobj = null; try { beanInfo = Introspector.getBeanInfo(タイプ); obj = type.newInstance();マップをJava Beanに変換すると、一部のプロパティを正しく設定できない
// 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.
は、例えば、私は豆、マップ内の データにマップを変換したい: A01-> 0.01; A02-> 0.02; AD - > "12345678" のJava Bean定義: プライベートダブルA01。プライベートDouble A02;プライベートString AD; セッターとゲッター。 ADは正しく設定できますが、A01とA02はnullになります。 – Victor