将json转换成bean

/**
     * data={"id":"1"}用json的数据创建指定的pojo.
     * 
     * @param 
     *            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 json2Bean(String data, Class 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 
     *            泛型
     * @return clazz实例
     * @throws Exception
     *             可能抛出任何异常
     */
    public static  T json2Bean(String data, Class clazz) throws Exception {
        return json2Bean(data, clazz, null, null);
    }

    /**
     * data={"id":"1"}用json里的数据,填充指定的pojo.
     * 
     * @param 
     *            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 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 
     *            泛型
     * @return 实例
     * @throws Exception
     *             可能抛出任何异常
     */
    public static  T json2Bean(String data, T entity) throws Exception {
        return json2Bean(data, entity, null, null);
    }

    /**
     * 根据Class生成entity,再把JSONObject中的数据填充进去.
     * 
     * @param 
     *            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 json2Bean(JSONObject jsonObject, Class 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 
     *            泛型
     * @return 实例
     * @throws Exception
     *             可能抛出任何异常
     */
    public static  T json2Bean(JSONObject jsonObject, Class clazz) throws Exception {
        return json2Bean(jsonObject, clazz, null, null);
    }

    /**
     * 把JSONObject中的数据填充到entity中.
     * 
     * @param 
     *            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 json2Bean(JSONObject jsonObject, T entity, String[] excludes, String datePattern)
            throws Exception {
        // JsonUtils.configJson(excludes, datePattern);
        Set 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 createExcludeSet(String[] excludes) {
        Set excludeSet = new HashSet();
        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;
    }

    /**
     * 
     * 
* 作者: lzy
* 创建时间: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); }


你可能感兴趣的:(java基础)