接口请求与对象转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);
相关推荐
皮卡龙4 小时前
Java常用的JSON
java·开发语言·spring boot·json
@#---9 小时前
如何准确判断json文件并且拿到我想要的信息
android·python·json
WarPigs13 小时前
Unity添加Newtonsoft.json
json
张彦峰ZYF20 小时前
巨大 JSON / 图结构数据架构层面选型:该放 Redis 还是 MongoDB?
redis·架构·json·巨大json/图结构架构选型·redis-mongodb
一颗不甘坠落的流星1 天前
【Antd】基于 Upload 组件,导入Json文件并转换为Json数据
前端·javascript·json
亮子AI1 天前
application/json 服务器收到的是字符串,还是json对象?
运维·服务器·json
im_AMBER2 天前
weather-app开发手记 02 JSON基础 | API 调用 400 错误修复 | JWT 认证问题
笔记·学习·json·axios·jwt
Irene19912 天前
Prettier 配置文件 .prettierrc.js 和 .prettierrc.json 的区别
javascript·json
23G3 天前
Fastjson转换jsonStr时设置SerializerFeature.DisableCircularReferenceDetect不生效
json
函数的彼端3 天前
iOS Model Generator - 让 JSON 转模型变得简单高效
ios·json·cocoa