完美解决FastJson出现 com.alibaba.fastjson.JSONObject cannot be cast to model的bug

 /**
     * 将list map转list
     *
     * @param maps  map
     * @param clazz clazz
     * @param <T>   T
     * @return list
     * @throws NoSuchMethodException
     * @throws IllegalAccessException
     * @throws InvocationTargetException
     * @throws InstantiationException
     */
    public static <T> List listMap2List(List<Map<Object, ? extends T>> maps, Class<? extends T> clazz) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {

        List<T> targets = null;

        if (V.notEmpty(maps)) {
            targets = new ArrayList<>();
            for (Map map : maps) {
                T target = clazz.getConstructor().newInstance();
                Field[] fields = clazz.getDeclaredFields();

                if (V.notEmpty(fields)) {
                    for (Object keyObj : map.keySet()) {

                        for (Field field : fields) {
                            if (keyObj != null && ((String) keyObj).equals(field.getName())) {
                                if (!Modifier.isPublic(field.getModifiers())) {
                                    field.setAccessible(true);
                                }
                                T value = (T) map.get(keyObj);

                                if (value != null) {
                                    try {
                                        field.set(target, convertValueToFieldType(value, field));
                                    } catch (Exception e) {
                                        log.info("{}字段尝试转换异常", field.getName());
                                    }
                                }
                            }
                        }
                    }
                }

                targets.add(target);
            }
        }

        return targets;
    }

    /**
     * jsonObj => list
     *
     * @param jsonObj jsonObj
     * @param clazz   clazz
     * @param <T>     T
     * @return
     * @throws NoSuchMethodException
     * @throws IllegalAccessException
     * @throws InvocationTargetException
     * @throws InstantiationException
     */
    public static <T> List jsonObj2List(Object jsonObj, Class<T> clazz) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        return listMap2List(JSON.parseObject(jsonObj.toString(), ArrayList.class), clazz);
    }
 /**
     * 转换为field对应的类型
     *
     * @param value
     * @param field
     * @return
     */
    public static Object convertValueToFieldType(Object value, Field field) throws ParseException {
        String type = field.getGenericType().getTypeName();
        if (value.getClass().getName().equals(type)) {
            return value;
        }

        if (String.class.getName().equals(type)) {
            return S.valueOf(value);
        } else if (Integer.class.getName().equals(type)) {
            return Integer.parseInt(S.valueOf(value));
        } else if (Long.class.getName().equals(type)) {
            return Long.parseLong(S.valueOf(value));
        } else if (Double.class.getName().equals(type)) {
            return Double.parseDouble(S.valueOf(value));
        } else if (BigDecimal.class.getName().equals(type)) {
            return new BigDecimal(S.valueOf(value));
        } else if (Float.class.getName().equals(type)) {
            return Float.parseFloat(S.valueOf(value));
        } else if (Boolean.class.getName().equals(type)) {
            return V.isTrue(S.valueOf(value));
        } else if (type.contains(Date.class.getSimpleName())) {
            if (value != null && value instanceof Long) {
                Date date = new Date((long) value);
                return date;
            }

            return D.fuzzyConvert(S.valueOf(value));
        }
        return value;
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值