Model class for Jackson Parser to parse Uppercase properties

后端 未结 4 789
生来不讨喜
生来不讨喜 2021-01-18 02:21

I have a json file which looks like this:

{
    \"ANIMALS\": {
    \"TYPE\": \"MAMMAL\",
    \"COLOR\": \"BLACK\",
    \"HEIGHT\": \"45\",

    }
}
         


        
4条回答
  •  执念已碎
    2021-01-18 03:06

    You should implement new naming strategy for your case:

    class LowerCaseNamingStrategy extends LowerCaseWithUnderscoresStrategy {
    
        private static final long serialVersionUID = 1L;
    
        @Override
        public String translate(String arg0) {
            return arg0.toUpperCase();
        }
    }
    

    After that, configure ObjectMapper

    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.setPropertyNamingStrategy(new LowerCaseNamingStrategy());
    

    See also @JsonProperty annotation.

    • Change field case with an ObjectMapper

提交回复
热议问题