JSON转换问题集合

List中不能直接转换成List,要先转换成List后Object再一个个转换成T

  JsonResult jsonResult = new JsonResult();
        List<Object> jsonList = null;
        try {
            jsonList = JSON.parseObject(json, List.class);
        } catch (Exception e) {
            log.error("错误提示:"+e.getMessage());
            throw new ApiException(JsonResultEnum.BASICSDATA_ONLEONE);
        }

        List<BomDto> bomErrorDtos = new LinkedList<>();
        for (Object o : jsonList) {
            BomDto bomDto = JSONObject.toJavaObject((JSON) o, BomDto.class);
            bomErrorDtos.add(bomDto);
        }

可以避免一个个转换而出现无法是被的情况:

List<BomDto> studentList1 = JSON.parseArray(JSON.parseObject(json), BomDto.class);

之前一个个转出现无法识别Date类型:

Caused by: com.alibaba.fastjson.JSONException: can not cast to Date, value : 2019-12-02T03:58:39.000+0000
发布了33 篇原创文章 · 获赞 0 · 访问量 1433

猜你喜欢

转载自blog.csdn.net/m0_46086429/article/details/104530732