内省Introspector实现Bean对象转Map

直接上代码,把对象放进去就能返回一个Map对象了

    private Map<String, Object> objectToMap(Object bindings) throws IntrospectionException, InvocationTargetException, IllegalAccessException {

        Map<String, Object> data = new HashMap();
        //内省 获取对象的信息
        BeanInfo beanInfo = Introspector.getBeanInfo(bindings.getClass());
        //获取特性
        PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
        //遍历特性
        for (PropertyDescriptor propertyDescriptor : propertyDescriptors){
            //获取属性名
            String key = propertyDescriptor.getName();
            if (key.equals("class")){
                continue;
            }
            //获取对应属性的get方法
            Method getter = propertyDescriptor.getReadMethod();
            //执行方法
            Object value = getter!=null ? getter.invoke(bindings) : null;
            data.put(key,value);
        }
        return data;
    }
当然还有一种情况是如果遇到类对象里面还有类对象怎么办?只需要在上面的方法中修改一下,添加一个递归

    private Map<String, Object> objectToMap(Object bindings) throws IntrospectionException, InvocationTargetException, IllegalAccessException {

        Map<String, Object> data = new HashMap();
        //内省 获取对象的信息
        BeanInfo beanInfo = Introspector.getBeanInfo(bindings.getClass());
        //获取特性
        PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
        //遍历特性
        for (PropertyDescriptor propertyDescriptor : propertyDescriptors){
            //获取属性名
            String key = propertyDescriptor.getName();
            if (key.equals("class")){
                continue;
            }
            //获取对应属性的get方法
            Method getter = propertyDescriptor.getReadMethod();
            //执行方法
            Object value = getter!=null ? getter.invoke(bindings) : null;
            //判断是否是对象中的对象
            if (judgeIsObjectInObject(value)){
                //如果是,则递归,返回Map
                value = objectToMap(value);
            }
            data.put(key,value);
        }
        return data;
    }
还有一个判断的方法要用到

    private boolean judgeIsObjectInObject(Object object) {
        boolean IsObjectInObject = false;
        //排除基本类型
        if (object instanceof Integer){
            return IsObjectInObject;
        }
        else if (object instanceof String){
            return IsObjectInObject;
        }
        else if (object instanceof Float){
            return IsObjectInObject;
        }
        else if (object instanceof Double){
            return IsObjectInObject;
        }else {
            IsObjectInObject = true;
        }
        return IsObjectInObject;
    }

另外如果要在android中使用,则需要导入openBean包,因为android阉割掉了Javabean的很多类




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值