接口请求与对象转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);
相关推荐
归途醉染15 小时前
Swifter.Json
c#·json·swifter.json
网络点点滴16 小时前
NPM 和 package.json 文件简介
前端·npm·json
夜瞬21 小时前
HTTP基础教程:请求方法、状态码、JSON、鉴权、超时、重试与流式返回
网络协议·http·json
wtsolutions2 天前
JSON-to-Excel 本地化应用发布:安全离线转换,数据零泄露
安全·json·excel
剑神一笑3 天前
从 JSON.parse 到树形视图:实现一个在线 JSON 格式化工具
前端·javascript·json
烤麻辣烫3 天前
json与fastjson
前端·javascript·学习·json
guslegend4 天前
AI生图第3节:gpt-image-2的提示词反解析与Json结构化生图
人工智能·gpt·json
wtsolutions4 天前
Excel-to-JSON Local App - Secure Offline Excel to JSON Conversion
json·excel
Hello_Embed4 天前
嵌入式上位机开发入门(二十九):JsonRPC TCP Server
网络·单片机·网络协议·tcp/ip·json·嵌入式