常用配置
ObjectMapper objectMapper = new ObjectMapper(); objectMapper.configure(Feature.IGNORE_UNKNOWN,true); objectMapper.configure(Feature.WRITE_BIGDECIMAL_AS_PLAIN,true); objectMapper.configure(JsonParser.Feature.ALLOW_MISSING_VALUES,true); objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,false); objectMapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES,false);//大小写脱敏 默认为false 需要改为tru
参考
com.fasterxml.jackson.databind.MapperFeature#ACCEPT_CASE_INSENSITIVE_PROPERTIES
使用注解方式:举例
public static void main(String[] args) throws IOException { String x = "{\n" + " \"TToUserName\":\"gh_a5624dd2db4e\",\n" + " \"FFromUserName\":\"ochvq0Kn35VlnTAcIJ3fRBAZTQUY\"" + " }"; ObjectMapper objectMapper = new ObjectMapper(); Result map = objectMapper.readValue(x, Result.class); System.out.println(map); objectMapper.writeValue(System.out,map); } private static class Result { private String ToUserName; private String FromUserName; @JsonProperty("ToUserName") public String getToUserName() { return ToUserName; } @JsonProperty("TToUserName") public void setToUserName(String toUserName) { ToUserName = toUserName; } @JsonProperty("FromUserName") public String getFromUserName() { return FromUserName; } @JsonProperty("FFromUserName") public void setFromUserName(String fromUserName) { FromUserName = fromUserName; } }
Jackson 转换大小写问题
Jackson转换json时会把大写转换成小写
解决办法:
1、在变量时加上: @JsonProperty
2、在set/get方法加上:@JsonIgnore
以上为个人经验,希望能给大家一个参考,也希望大家多多支持。