当前位置: 首页 > news >正文

hutool工具类JSONUtil无法映射全是大写的单词,如何解决

背景

在解析第三方接口数据时,发现有的字段数据没有映射到对应的字段上,还有对于有的字段有空格或换行,也会一同存入数据库。

示例

实体类:

public class Goods {

private String id;private String unit;private Integer USER_NUM;private Integer isValid;private String isDelete;
//...

}

测试方法:

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.nxg.util.dateutils.DateUtils;
import org.junit.Test;
import org.springframework.boot.test.context.SpringBootTest;

//省略…

@Test
public void test_json() {
String jtr = “{\n” +
““code”: 0,\n” +
““data”: “[{\“ID\”:\“12\”,\“UNIT\”:\“元\”,\“USER_NUM\”:100,\“IS_VALID\”:2,\“IS_DELETE\”:\“N\”},{\“ID\”:\“13\”,\“UNIT\”:\“次\”,\“USER_NUM\”:100,\“IS_VALID\”:2,\“IS_DELETE\”:\“N\”}]”,\n” +
““message”: “success”\n” +
“}”;

    JSONObject entries = JSONUtil.parseObj(jtr);JSONArray data = JSONUtil.parseArray(entries.get("data"));List<Goods> goodsList = new ArrayList<>();data.forEach(item -> {JSONObject entries1 = JSONUtil.parseObj(item);Goods bean = JSONUtil.toBean(entries1, Goods.class);goodsList.add(bean);});System.out.println(goodsList);
}

解决方法

方法1:

使用fastjson 解决 (推荐)

方法2:

取出值,再set到实体类中

如果字段太多,就很麻烦,还容易遗漏

方法3

暂时没有


http://www.mrgr.cn/news/13371.html

相关文章:

  • 【处理音频需求】Python的webrtcvad库配置
  • kafka监听
  • 用宝塔部署项目到阿里云服务器访问不到的问题
  • MySQL 的半同步模式
  • MyBatis-Plus联表查询(mybatis-plus-join)
  • 实习结束总结20240828
  • LeetCode面试题Day18|LC61 旋转链表
  • Python TensorFlow入门与实践
  • Celery 中,广播模式可以通过使用 RabbitMQ 的 fanout 交换机来实现
  • JS脚本实现RPA模拟人工操作网页获取数据
  • 登录失败时刷新验证码
  • Avalonia与WPF开发时的差异总结
  • C语言基础(十二)
  • 美国短剧APP借力Facebook广告引流核心优势攻略
  • const、inline、nullptr的使用
  • Spring框架 基础介绍
  • SpringBoot核心配置文件(SpringBoot学习3)
  • 结合令牌(JWT)和签名认证的系统登录及页面访问的详细实现原理和流程
  • APP渠道来源方案探索
  • Docker培训