简体   繁体   English

使JsonGetter不区分大小写

[英]Make JsonGetter case insensitive

I'm using JacksonAnnotation along with Spring Framework to parse a JSON that I get from a web service for my app. 我正在使用JacksonAnnotationSpring Framework来解析我从我的应用程序的Web服务获得的JSON。

I have the same data structure coming from two distinct methods but there is a field in one of them that comes capitalized. 我有两个不同方法的相同数据结构,但其中一个中有一个字段大写。 I don't want to create two data structures just because of that. 我不想因此而创建两个数据结构。

Is there any way that I make JsonGetter case insensitive or at least make it accept two version of a string? 有没有办法让JsonGetter不区分大小写,或者至少让它接受两个版本的字符串?

Currently I have to use this for method A 目前我必须将它用于方法A.

@JsonGetter("CEP")
public String getCEP() {
    return this.cep;
}

and this for method B 这对方法B来说

@JsonGetter("Cep")
public String getCEP() {
    return this.cep;
}

Thanks 谢谢

You can create new setter method for each possibility of property name: 您可以为属性名称的每种可能性创建新的setter方法:

import com.fasterxml.jackson.annotation.JsonSetter;
import com.fasterxml.jackson.databind.ObjectMapper;

public class JacksonProgram {

    public static void main(String[] args) throws Exception {
        ObjectMapper mapper = new ObjectMapper();
        System.out.println(mapper.readValue("{\"Cep\":\"value\"}", Entity.class));
        System.out.println(mapper.readValue("{\"CEP\":\"value\"}", Entity.class));
    }
}

class Entity {

    private String cep;

    public String getCep() {
        return cep;
    }

    @JsonSetter("Cep")
    public void setCep(String cep) {
        this.cep = cep;
    }

    @JsonSetter("CEP")
    public void setCepCapitalized(String cep) {
        this.cep = cep;
    }

    @Override
    public String toString() {
        return "Entity [cep=" + cep + "]";
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM