当前位置: 首页> 科技> 数码 > hutool工具类JSONUtil无法映射全是大写的单词,如何解决

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

时间:2025/7/9 5:22:33来源:https://blog.csdn.net/nxg0916/article/details/141561112 浏览次数:0次

背景

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

示例

实体类:

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

暂时没有

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

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: