Fastjson首字母大小写问题

news/2024/10/5 5:54:05/

1、问题

使用Fastjson转json之后发现首字母小写。实体类如下:

java">@Data
public class DataIdentity {private String BYDBSM;private String SNWRSSJSJ;private Integer CJFS = 20;
}

测试代码如下:

java">public static void main(String[] args) {DataIdentity dataIdentity = new DataIdentity();dataIdentity.setBYDBSM("xxx");dataIdentity.setSNWRSSJSJ(DateUtil.format(LocalDateTime.now(), "yyyy-MM-dd HH:mm:ss"));String str = JSON.toJSONString(dataIdentity);System.out.println(str);
}

测试结果如下:

2、分析

通过查看Fastjson源码可知,Fatjson在序列化对象时,会判断compatibleWithJavaBean,如果为false则将首字母小写,compatibleWithJavaBean默认值为false.

java">public class TypeUtils {private static final Pattern NUMBER_WITH_TRAILING_ZEROS_PATTERN = Pattern.compile("\\.0*$");public static boolean compatibleWithJavaBean = false;public static boolean compatibleWithFieldName = false;
...
}
java">...
if (Character.isUpperCase(c2)) {if (compatibleWithJavaBean) {propertyName = decapitalize(methodName.substring(2));} else {propertyName = Character.toLowerCase(methodName.charAt(2)) + methodName.substring(3);}
propertyName = getPropertyNameByCompatibleFieldName(fieldCacheMap, methodName, propertyName, 2);
...

3、解决方案

1.compatibleWithJavaBean设置为true

java">TypeUtils.compatibleWithJavaBean = true;

也可以通过设置jvm参数。

2.@JSONField注解

java">@Data
public class DataIdentity {@JSONField(name = "BYDBSM")private String BYDBSM;@JSONField(name = "SNWRSSJSJ")private String SNWRSSJSJ;@JSONField(name = "CJFS")private Integer CJFS = 20;}

3、使用hutool的JSONUtil.toJsonStr()方法

java">String str = JSONUtil.toJsonStr(yytStuCountDto);

4、参考文章

Fastjson首字母大小写问题_fastjson 首字母小写-CSDN博客

https://www.cnblogs.com/haoxianrui/p/12853343.html

fastjson转换json时,碰到的那些首字母大小写转换的坑! - 简书


http://www.ppmy.cn/news/1473684.html

相关文章

MongoDB 单节点升级为副本集高可用集群(1主1从1仲裁)

作者介绍:老苏,10余年DBA工作运维经验,擅长Oracle、MySQL、PG、Mongodb数据库运维(如安装迁移,性能优化、故障应急处理等) 公众号:老苏畅谈运维 欢迎关注本人公众号,更多精彩与您分享…

集智书童 | 英伟达和斯坦福基于 Transformer 的异常检测最新研究!

本文来源公众号“集智书童”,仅用于学术分享,侵权删,干货满满。 原文链接:英伟达和斯坦福基于 Transformer 的异常检测最新研究! 在作者推动各种视觉任务性能边界的同时,模型的大小也在相应增长。为了跟上…

自动化一些操作

下拉选择框 from selenium import webdriver from time import sleep # 导包 from selenium.webdriver.support.select import Select driver webdriver.Edge() driver.get(r"D:\WORK\ww\web自动化_day01_课件笔记资料代码\web自动化_day01_课件笔记资料代码\02_其他资料…

JCEF 在idea 开发 java 应用

JCEF(Java Chromium Embedded Framework)是一个Java库,用于在Java应用程序中嵌入Chromium浏览器引擎。如果您想在IDEA开发环境中使用JCEF,您可以按照以下步骤进行操作: 1. 下载JCEF库文件:您可以从JCEF的官…

KIVY 3D Rotating Monkey Head¶

7 Python Kivy Projects (With Full Tutorials) – Pythonista Planet KIVY 3D Rotating Monkey Head kivy 3D 旋转猴子头How to display rotating monkey example in a given layout. Issue #6688 kivy/kivy GitHub 3d 模型下载链接 P99 - Download Free 3D model by …

【算法笔记自学】第 6 章 C++标准模板库(STL)介绍

6.1vector常见用法详解 #include <cstdio> #include <vector> using namespace std;int main() {int n, x;scanf("%d", &n);vector<int> v;for (int i 0; i < n; i) {scanf("%d", &x);v.push_back(x);}for (int i 0; i <…

优化路由,优化请求url

1、使用父子关系调整下使其更加整洁 2、比如说我修改了下url,那所有的页面都要更改 优化&#xff1a;把这个url抽出来&#xff0c;新建一个Api文件夹用于存放所有接口的url&#xff0c;在业务里只需要关注业务就可以 使用时 导包 发请求 如果想要更改路径&#xff0c;在这里…

Go 语言多版本管理的最佳实践 —— Linux 和 Windows 专题20240702

Go 语言多版本管理的最佳实践 —— Linux 和 Windows 专题 引言 在软件开发的世界里&#xff0c;保持开发环境的最新和兼容至关重要。特别是 Go 语言&#xff0c;随着版本的更新&#xff0c;不同项目可能需要不同的 Go 版本。这时&#xff0c;如何在同一台机器上高效管理多个…