接口请求与对象转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 天前
WCF+JSON+实体对象与WebService+DataSet效率大比拼
数据库·oracle·json
PixelBai1 天前
JSON转XML使用教程:从入门到精通
xml·json
知兀1 天前
【构建后的文件目录】vite构建项目的基本文件;React文件
json
倒流时光三十年2 天前
PostgreSQL to_json 与 row_to_json:妙用与区别详解
数据库·postgresql·json
PixelBai2 天前
JSON转表格使用教程:从入门到精通
json
殳翰2 天前
向客户端提供JSON数据的方式
okhttp·json
bugcome_com2 天前
JSON 知识小课堂
json
PixelBai2 天前
JSON转表格实际应用场景案例
json
ShiXZ2133 天前
PDF-OCR文件识别篇(七):数据入库
java·pdf·json·ocr·springboot
bloxed3 天前
大模型应用-筑基期【11:JSON结构化输出实战】
json·大模型应用