接口请求与对象转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);
相关推荐
小高Baby@8 小时前
JSON、bind、form
数据结构·json
阿蒙Amon10 小时前
TypeScript学习-第11章:配置文件(tsconfig.json)
学习·typescript·json
一起养小猫13 小时前
Flutter for OpenHarmony 进阶:Timer组件与倒计时系统深度解析
android·网络·笔记·flutter·json·harmonyos
女王大人万岁15 小时前
Go语言JSON标准库(encoding/json):功能解析与实战指南
服务器·开发语言·后端·golang·json
每天要多喝水1 天前
nlohmann/json 的使用
c++·json
电商API&Tina1 天前
乐天平台 (Rakuten) 数据采集指南
大数据·开发语言·数据库·oracle·json
数研小生2 天前
构建命令行单词记忆工具:JSON 词库与艾宾浩斯复习算法的完美结合
算法·json
编码者卢布2 天前
【Azure Stream Analytic】用 JavaScript UDF 解决 JSON 字段被转成 Record 的关键点
javascript·json·azure
电商API&Tina2 天前
电商数据采集 API 接口 全维度解析(技术 + 商业 + 合规)
java·大数据·开发语言·数据库·人工智能·json
liwulin05062 天前
【JSON】使用com.fasterxml.jackson解析json字符串
java·数据库·json