接口请求与对象转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);
相关推荐
wtsolutions5 小时前
Understanding Excel Data Formats - What Excel to JSON Supports
ui·json·excel
wtsolutions8 小时前
Real-World Use Cases - How Organizations Use Excel to JSON
json·github·excel
cab58 小时前
MyBatis如何处理数据库中的JSON字段
数据库·json·mybatis
FITA阿泽要努力9 小时前
动手体验:5min实现第一个智能体——1
json·api·agent·requests·查询天气·pprint·f-string
wtsolutions9 小时前
Using the Excel to JSON API - Programmatic Access for Developers
ui·json·xhtml
石云升10 小时前
Claude Code 配置教程:如何通过修改 settings.json 优化 AI 编程体验
人工智能·json
wtsolutions11 小时前
MCP Service Integration - Excel to JSON for AI and Automation
人工智能·json·excel
cjp56011 小时前
019.C#管道服务,两软件间用json数据交互
开发语言·c#·json
组合缺一11 小时前
FastJson2 与 SnackJson4 有什么区别?
java·json·fastjson·snackjson