首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将Map转换为Java,某些属性无法正确设置

将Map转换为Java,某些属性无法正确设置
EN

Stack Overflow用户
提问于 2016-11-23 03:12:26
回答 1查看 247关注 0票数 1

//我使用这个简单的程序:公共静态对象convertToBean(类类型,映射){ BeanInfo beanInfo;对象obj =空;尝试{ beanInfo = Introspector.getBeanInfo( type );obj = type.newInstance();

代码语言:javascript
复制
            // 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.
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-12-09 03:35:32

最后我找到了根本原因。问题通过将"a01“改为”a01“来解决。变量名必须是严格的骆驼规则。第一个字符必须是小写,除了前两个字符都是大写字母,比如"AD“。因为setter和getter方法将以相同的模式生成。所以很难识别出一个变量的真名。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40755375

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档