接口请求与对象转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);
相关推荐
紧跟先前的步伐1 天前
Go AI Agent 实战 11|请求结构、响应结构与 JSON 解析
人工智能·ai·golang·json
骑着蜗牛撵大象3271 天前
用 Codex 批量做数字人商品口播视频:从分镜 JSON 到成片校验的流水线
java·ffmpeg·json·音视频·数字人·视频·codex
外收内放1 天前
FastAPI猜字谜实战(3):把每一局存成JSON
开发语言·python·学习·json·fastapi
一只旭宝1 天前
JSON 简介
json
数据狐(Datafox)2 天前
一文读懂京东商品详情API:返回字段解读与 JSON 样例
java·开发语言·数据库·windows·json
喜欢喝果茶.2 天前
JSON / CBOR / Protobuf区别
mqtt·json
江屿风2 天前
【Plain Language Large Model】【大语言模型底层原理全解析:从Token生成到智能体演进】流食般投喂
人工智能·笔记·语言模型·llm·json·token·聊天模板
情 九3 天前
Dify HTTP 请求节点实战:发送 JSON、解析响应与处理异常
网络协议·http·json·dify
qq_白羊座3 天前
口头说的 JSON 和 MySQL 里的 JSON
数据库·mysql·json
love静思冥想3 天前
CLOB中JSON查询agent字段值(Oracle11g)
sql·oracle·json