Java对象蛇形命名与驼峰命名互转
接口定义是蛇形命名,实体类定义为驼峰
直接上demo
主要依靠这句:@JSONType(naming= PropertyNamingStrategy.SnakeCase)
import com.alibaba.fastjson.PropertyNamingStrategy;
import com.alibaba.fastjson.annotation.JSONType;
/**
* 要序列化的类
* @author KP
* @date 2021/6/29
*/
@JSONType(naming= PropertyNamingStrategy.SnakeCase)
public class TestJSON {
private String aBcD;
public String getaBcD() {
return aBcD;
}
public void setaBcD(String aBcD) {
this.aBcD = aBcD;
}
}
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
/**
* 测试方法
* @author KP
* @date 2021/6/29
*/
public class Test {
public static void main(String[] args) {
TestJSON testJSON = new TestJSON();
testJSON.setaBcD("aaa");
System.out.println(JSONObject.toJSON(testJSON));
String json = "{\"a_bc_d\":\"aaa\"}";
TestJSON testJSON1 = JSON.parseObject(json, TestJSON.class);
System.out.println(JSONObject.toJSON(testJSON1));
}
}
测试结果:
感谢以下文章
Spring Boot 请求参数自动注入实体,格式转换(snake_case转camelCase)
java 对象转蛇形命名法(snake case)形式的 map
fastjson属性名设置为PascalCase、SnakeCase、KebabCase策略的正确姿势