我想将bean类转换为map(key =成员的名称,value =成员的值)。
我正在使用方法BeanUtils.describe(beanClass);
(编辑:我正在使用commons-beanutils 1.8.3,jdk 1.6.0_20,在commons-beanutils 1.5上工作)
问题是返回值不正确,(地图只包含数组中的第一项),
代码:
public class Demo {
private ArrayList<String> myList = new ArrayList<String>();
public Demo() {
myList.add("first_value");
myList.add("second_value");
}
public ArrayList<String> getMyList() {
return myList;
}
public void setMyList(ArrayList<String> myList) {
this.myList = myList;
}
public static void main(String[] args) {
Demo myBean = new Demo();
try {
Map describe = BeanUtils.describe(myBean);
Iterator it = describe.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pairs = (Map.Entry) it.next();
System.out.println(String.format("key=%s,value=%s", (String) pairs.getKey(), (String) pairs.getValue()));
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
键= myList中,值= [FIRST_VALUE,SECOND_VALUE]
key = class,value = class $ Demo
键= myList中,值= [FIRST_VALUE]
key = class,value = class $ Demo
正如您所看到的,数组包含两个值,但输出(和地图)只包含一个,为什么??
谢谢,
尼
答案 0 :(得分:0)
我在计算机上运行代码示例,输出为: key = myList,value = [first_value,second_value] key = class,value = class com.gpdi.infores.dao.test.Demo 使用JDK5或更高版本就可以了。