接口请求与对象转json中字段大小写的处理

1、前端请求对象中,字段为大写的情况

》前端请求对象字段为大写

》后端接受对象字段也为大写

前后端对象字段一模一样,就是接受不到前端传过来的值,针对这种情况,只需在后端对象中加@JsonProperty("Id")即可

如下所示:

java 复制代码
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;

@Data
public class AfterResume {
    @JsonProperty("Id")
    private String Id;
    /**
     * 起止时间
     */
    @JsonProperty("YearAndMonth")
    private String YearAndMonth;

    /**
     * 在何单位任职
     */
    @JsonProperty("OrganizationAndJob")
    private String OrganizationAndJob;
}

2、对象转json字符串,但对象中存在字段大写的情况,想要保持对象字段和json字符串格式一直;就不能用fastjson。这里有2中方式可供转换

1)通过com.fasterxml.jackson.databind.ObjectMapper去转换,具体代码如下

java 复制代码
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
 
public class Main {
    public static void main(String[] args) throws Exception {
        User user = new User("John", "Doe", 25);
        
        ObjectMapper mapper = new ObjectMapper();
        mapper.setPropertyNamingStrategy(PropertyNamingStrategy.UPPER_CAMEL_CASE); // 设置首字母大写的策略
        
        String json = mapper.writeValueAsString(AfterResume);
        System.out.println(json);
    }
}

2、 第二种通过gson去转换

java 复制代码
String json = new Gson().toJson(afterResumes);
System.out.println(json);
相关推荐
莫叫石榴姐4 小时前
ast 在 Dify 工作流中解析 JSON 格式数据的深度解析
大数据·网络·安全·json
GDAL4 小时前
express.json 深入全面讲解教程
json·express
武藤一雄6 小时前
彻底吃透.NET中序列化反序列化
xml·微软·c#·json·.net·.netcore
liangshanbo12156 小时前
JSON-RPC 2.0 协议详解
网络协议·rpc·json
elangyipi1231 天前
深入理解前端项目中的 package.json 和 package-lock.json
前端·json
雪域迷影1 天前
Windows11中VS2026使用C++ 现代化json库nlohmann的3种方式
开发语言·c++·json
雪域迷影1 天前
nlohmann::json库对象和json结构体转换的新方式
c++·json·nlohmann_json库
生产队队长2 天前
Web:免费的JSON接口
json
Honmaple2 天前
中国四级城市联动数据,包含港澳台,内含json , sql , python 脚本
python·sql·json
慧都小项2 天前
Parasoft Jtest 如何用 JSON 文件驱动Java 测试自动化
java·自动化·json