fastjson2中Json下划线转Java驼峰命名

前言

有一个配置文件如下:

{
    
    
  "server_type": "Java",
  "client_type": "CSharp"
}

对应的类如下:

public class ConvertConfig {
    
    
    public String serverType;
    public String clientType;
}

未特殊处理的解析的方式为:

JSONObject.parseObject(Files.readString(path), clazz);

解决方案

方案一: JsonField

修改类配置:

public class ConvertConfig {
    
    
    @JSONField(name = "server_type")
    public String serverType;

    @JSONField(name = "client_type")
    public String clientType;
}

方案二:Feature

解析时传入特性,支持智能匹配:

JSONObject.parseObject(Files.readString(path), clazz, Feature.SupportSmartMatch);

猜你喜欢

转载自blog.csdn.net/sayWhat_sayHello/article/details/129155367