将json转换成bean

/**
     * data={"id":"1"}用json的数据创建指定的pojo.
     * 
     * @param <T>
     *            Object
     * @param data
     *            json字符串
     * @param clazz
     *            需要转换成bean的具体类型
     * @param excludes
     *            不需要转换的属性数组
     * @param datePattern
     *            日期转换模式
     * @return T
     * @throws Exception
     *             java.lang.InstantiationException, java.beans.IntrospectionException, java.lang.IllegalAccessException
     */
    public static <T extends Object> T json2Bean(String data, Class<T> clazz, String[] excludes, String datePattern)
            throws Exception {
        // JsonUtils.configJson(excludes, datePattern);
        T entity = clazz.newInstance();

        return json2Bean(data, entity, excludes, datePattern);
    }

    /**
     * json转换成bean.
     * 
     * @param data
     *            json字符串
     * @param clazz
     *            需要转换的类型
     * @param <T>
     *            泛型
     * @return clazz实例
     * @throws Exception
     *             可能抛出任何异常
     */
    public static <T extends Object> T json2Bean(String data, Class<T> clazz) throws Exception {
        return json2Bean(data, clazz, null, null);
    }

    /**
     * data={"id":"1"}用json里的数据,填充指定的pojo.
     * 
     * @param <T>
     *            Object
     * @param data
     *            json字符串
     * @param entity
     *            需要填充数据的bean
     * @param excludes
     *            不需要转换的属性数组
     * @param datePattern
     *            日期转换模式
     * @return T
     * @throws Exception
     *             java.lang.InstantiationException, java.beans.IntrospectionException, java.lang.IllegalAccessException
     */
    public static <T extends Object> T json2Bean(String data, T entity, String[] excludes, String datePattern)
            throws Exception {
        // JsonUtils.configJson(excludes, datePattern);
        JSONObject jsonObject = JSONObject.fromObject(data);

        return json2Bean(jsonObject, entity, excludes, datePattern);
    }

    /**
     * json转换成bean.
     * 
     * @param data
     *            json字符串
     * @param entity
     *            实例
     * @param <T>
     *            泛型
     * @return 实例
     * @throws Exception
     *             可能抛出任何异常
     */
    public static <T extends Object> T json2Bean(String data, T entity) throws Exception {
        return json2Bean(data, entity, null, null);
    }

    /**
     * 根据Class生成entity,再把JSONObject中的数据填充进去.
     * 
     * @param <T>
     *            Object
     * @param jsonObject
     *            json对象
     * @param clazz
     *            需要转换成bean的具体类型
     * @param excludes
     *            不需要转换的属性数组
     * @param datePattern
     *            日期转换模式
     * @return T
     * @throws Exception
     *             java.lang.InstantiationException, java.beans.IntrospectionException, java.lang.IllegalAccessException
     */
    public static <T extends Object> T json2Bean(JSONObject jsonObject, Class<T> clazz, String[] excludes,
            String datePattern) throws Exception {
        // JsonUtils.configJson(excludes, datePattern);
        T entity = clazz.newInstance();

        return json2Bean(jsonObject, entity, excludes, datePattern);
    }

    /**
     * json转换成bean.
     * 
     * @param jsonObject
     *            JSONObject
     * @param clazz
     *            类型
     * @param <T>
     *            泛型
     * @return 实例
     * @throws Exception
     *             可能抛出任何异常
     */
    public static <T extends Object> T json2Bean(JSONObject jsonObject, Class<T> clazz) throws Exception {
        return json2Bean(jsonObject, clazz, null, null);
    }

    /**
     * 把JSONObject中的数据填充到entity中.
     * 
     * @param <T>
     *            Object
     * @param jsonObject
     *            json对象
     * @param entity
     *            需要填充数据的node
     * @param excludes
     *            不需要转换的属性数组
     * @param datePattern
     *            日期转换模式
     * @return T
     * @throws Exception
     *             java.lang.InstantiationException, java.beans.IntrospectionException, java.lang.IllegalAccessException
     */
    @SuppressWarnings("rawtypes")
    public static <T extends Object> T json2Bean(JSONObject jsonObject, T entity, String[] excludes, String datePattern)
            throws Exception {
        // JsonUtils.configJson(excludes, datePattern);
        Set<String> excludeSet = createExcludeSet(excludes);

        for (Object object : jsonObject.entrySet()) {
            Map.Entry entry = (Map.Entry) object;
            String propertyName = entry.getKey().toString();

            if (excludeSet.contains(propertyName)) {
                continue;
            }

            String propertyValue = entry.getValue().toString();

            try {
                PropertyDescriptor propertyDescriptor = new PropertyDescriptor(propertyName, entity.getClass());
                Class propertyType = propertyDescriptor.getPropertyType();
                Method writeMethod = propertyDescriptor.getWriteMethod();
                invokeWriteMethod(entity, writeMethod, propertyType, propertyValue, datePattern);
            } catch (IntrospectionException ex) {
                logger.info(entity.getClass() + ":" + ex.getMessage());
                continue;
            }
        }

        return entity;
    }

    /**
     * 配置排除列表.
     * 
     * @param excludes
     *            String[]
     * @return exclude set
     */
    public static Set<String> createExcludeSet(String[] excludes) {
        Set<String> excludeSet = new HashSet<String>();
        if (excludes != null) {
            for (String exclude : excludes) {
                excludeSet.add(exclude);
            }
        } else {
            excludeSet.add("hibernateLazyInitializer");
        }

        return excludeSet;
    }

    /**
     * 根据类型,反射调用setter方法.
     * 
     * @param entity
     *            实例
     * @param writeMethod
     *            setter方法
     * @param propertyType
     *            数据类型
     * @param propertyValue
     *            数据值
     * @param datePattern
     *            日期格式
     * @throws IntrospectionException
     *             methed
     * @throws Exception
     *             e
     */
    @SuppressWarnings("rawtypes")
    public static void invokeWriteMethod(Object entity, Method writeMethod, Class propertyType, String propertyValue,
            String datePattern) throws IntrospectionException, Exception {
        try {
            /** 如果参数为空 则 不再做处理 **/
            if (!StringUtil.notNullorEmpty(propertyValue)) {
                return;
            }
            /** 验证八个基本类型 **/
            if (isPrimivite(propertyType)) {
                invokePrimivite(entity, writeMethod, propertyType, propertyValue);
                /** 验证String类型 **/
            } else if (propertyType == String.class) {
                writeMethod.invoke(entity, propertyValue);
                /** 验证date类型 **/
            } else if (propertyType == Date.class && StringUtil.notNullorEmpty(propertyValue) && !"null".equals(propertyValue)) {
                SimpleDateFormat dateFormat = getDateFormat(datePattern);
                /** 如果datePattern为空,SimpleDateFormat 的默认格式为 yyyy-MM-dd T HH:mm:ss **/
                try {
                    /** 如果 采用默认格式 则 容易转换失败 如:(2011-11-11) **/
                    writeMethod.invoke(entity, dateFormat.parse(propertyValue));
                } catch (ParseException e) {
                    /** 如果 转换格式失败 则 采用年月日格式 **/
                    writeMethod.invoke(entity, getDateFormat("yyyy-MM-dd").parse(propertyValue));
                }
            }
        } catch (IntrospectionException e) {
            /** 转为bean的json信息 里包含了大量其他参数信息 没有找到weiterMethod将很常见 此处起提示作用 **/
            throw new IntrospectionException("没有找到" + writeMethod + "方法!");
        } catch (Exception exception) {
            /** 如果包含其他异常将记录并向上抛出 **/
            logger.error(exception);
            throw new Exception(exception);
        }

    }

    /**
     * 处理基本类型.
     * 
     * @param entity
     *            实例
     * @param writeMethod
     *            setter方法
     * @param propertyType
     *            数据类型
     * @param propertyValue
     *            数据值
     * @throws Exception
     *             异常
     */
    @SuppressWarnings("rawtypes")
    public static void invokePrimivite(Object entity, Method writeMethod, Class propertyType, String propertyValue)
            throws Exception {
        if (isByte(propertyType)) {
            writeMethod.invoke(entity, Byte.parseByte(propertyValue));
        } else if (isShort(propertyType)) {
            writeMethod.invoke(entity, Short.parseShort(propertyValue));
        } else if (isInt(propertyType)) {
            writeMethod.invoke(entity, Integer.parseInt(propertyValue));
        } else if (isLong(propertyType)) {
            writeMethod.invoke(entity, Long.parseLong(propertyValue));
        } else if (isFloat(propertyType)) {
            writeMethod.invoke(entity, Float.parseFloat(propertyValue));
        } else if (isDouble(propertyType)) {
            writeMethod.invoke(entity, Double.parseDouble(propertyValue));
        } else if (isBoolean(propertyType)) {
            writeMethod.invoke(entity, Boolean.parseBoolean(propertyValue));
        } else if (isChar(propertyType)) {
            writeMethod.invoke(entity, propertyValue.charAt(0));
        }
    }

    /**
     * 是否为八个基本类型.
     * 
     * @param clazz
     *            类型
     * @return boolean
     */
    @SuppressWarnings("rawtypes")
    public static boolean isPrimivite(Class clazz) {
        if (isByte(clazz)) {
            return true;
        } else if (isShort(clazz)) {
            return true;
        } else if (isInt(clazz)) {
            return true;
        } else if (isLong(clazz)) {
            return true;
        } else if (isFloat(clazz)) {
            return true;
        } else if (isDouble(clazz)) {
            return true;
        } else if (isBoolean(clazz)) {
            return true;
        } else if (isChar(clazz)) {
            return true;
        }

        return false;
    }

    /**
     * 
     * <br>
     * <b>作者: lzy</b> <br>
     * 创建时间:2012-3-30 上午10:44:42
     * 
     * @since 1.0
     * @param clazz
     *            传递过来的类型
     * @return bollean
     */
    public static boolean isBigDecimal(@SuppressWarnings("rawtypes") Class clazz) {
        return clazz == BigDecimal.class;
    }

    /**
     * 是否为byte类型.
     * 
     * @param clazz
     *            类型
     * @return boolean
     */
    @SuppressWarnings("rawtypes")
    public static boolean isByte(Class clazz) {
        return (clazz == Byte.class) || (clazz == byte.class);
    }


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值