简体   繁体   English

BeanUtils-“描述”方法返回不正确的数组值

[英]BeanUtils - 'describe' method returning Incorrect array value

I want to convert a bean class to a map (key=the name of the member,value=the value of the member). 我想将bean类转换为映射(键=成员的名称,值=成员的值)。

I'm using the method BeanUtils.describe(beanClass); 我正在使用方法BeanUtils.describe(beanClass);

( Edit :I'm using commons-beanutils 1.8.3, jdk 1.6.0_20, on commons-beanutils 1.5 it works) 编辑 :我在commons-beanutils 1.5上使用commons-beanutils 1.8.3,jdk 1.6.0_20,它有效)

The problem is that the return value is incorrect, (the map contain only the first item from the array), 问题在于返回值不正确(地图仅包含数组中的第一项),

the code: 编码:

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();
            }
        }
    }
  • The expected output: 预期输出:

key=myList,value= [first_value,second_value] key = myList,value = [first_value,second_value]

key=class,value=class $Demo 键=类别,值=类别$ Demo

  • But the real output is: 但是实际输出是:

key=myList,value= [first_value] key = myList,value = [first_value]

key=class,value=class $Demo 键=类别,值=类别$ Demo

As you can see the array contains two values but the output(and the map) contains only one,why?? 如您所见,数组包含两个值,但输出(和映射)仅包含一个值,为什么?

Thanks, 谢谢,

Benny 本尼

我在计算机上运行代码示例,输出为:key = myList,value = [first_value,second_value] key = class,value = class com.gpdi.infores.dao.test.Demo使用JDK5或更高版本即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM