2016-10-25 2 views
1

나는 jackson-core-2.8.3으로 작업하고 있으며 여러 경우에 제공되는 요소가있는 json을 사용하고 있습니다. 내 클래스에 매핑하고 싶지만 클래스에 PropertyNamingStratergy 유형을 하나만 가질 수 있기 때문에 그렇게 할 수 없습니다.잭슨과 여러 경우를 deserialize하는 방법

예 JSON은 : - :

class MyClass { 
public String tableKey; 
public Integer notAllowedPwd; 
} 

ObjectMapper 코드 : - -

{"tableKey": "1","not_allowed_pwd": 10} 

{"tableKey": "1","notAllowedPwd": 10} 

ClassToMap 같은 다른 JSON가있을 수 있습니다

ObjectMapperobjectMapper=new ObjectMapper(); 
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,false); 
objectMapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES,true); 
objectMapper.setSerializationInclusion(Include.NON_NULL); 
objectMapper.setVisibility(PropertyAccessor.ALL,Visibility.NONE); 
objectMapper.setVisibility(PropertyAccessor.FIELD,Visibility.ANY); 
MyClass obj = objectMapper.readValue(s, MyClass.class); 

어디서나 해결책을 찾을 수 없습니다. 누군가가 진행하는 방법을 도울 수 있다면 좋을 것입니다.

+0

보세요 http://stackoverflow.com/questions/12583638/when-is-the-jsonproperty-property-used-and-what-is-it-used-for –

+0

이 링크는 http : /websystique.com/java/json/jackson-json-annotations-example/ –

+0

문제는 json이 호출 전화가 다를 수 있으므로 snakecase가 camelcase 형식이 될 수 있습니다. – Swaraj

답변

0

다음과 같이 jackson-annotations 라이브러리를 사용하고 @JsonProperty을 추가하십시오.

class MyClass { 
    public String tableKey; 
    @JsonProperty("not_allowed_pwd") 
    public Integer notAllowedPwd; 
} 
+0

고마워요.하지만 다른 요구 사항이 있습니다. 질문을 편집했습니다. – Swaraj

+0

그런 경우에는 1) 두 개의 다른 클래스에 매핑하고 수동으로 결합하거나 2) json의 스키마를 수정해야합니다. 나는 변경/일관성없는 속성 이름을 자연스럽게 매핑 할 수 있다고 생각하지 않는다. – kjsebastian

+0

readValue 메서드에서 사용되는 클래스가있어서 클래스 이름을 camelcase 클래스로 수정할 수있다. – Swaraj

0

당신은 두 번째 필드 이름에 대한 @JsonProperty 주석과 두 번째 세터를 가질 수 있습니다

class MyClass { 
    private String tableKey; 
    private Integer notAllowedPwd; 

    public String getTableKey() { 
     return tableKey; 
    } 

    public void setTableKey(String tableKey) { 
     this.tableKey = tableKey; 
    } 

    public Integer getNotAllowedPwd() { 
     return notAllowedPwd; 
    } 

    public void setNotAllowedPwd(Integer notAllowedPwd) { 
     this.notAllowedPwd = notAllowedPwd; 
    } 

    @JsonProperty("not_allowed_pwd") 
    public void setNotAllowedPwd2(Integer notAllowedPwd) { 
     this.notAllowedPwd = notAllowedPwd; 
    } 
} 

이 두 가지 속성이 JSON에 존재하는 경우, 그들은 덮어 쓸 것을 고려.

관련 문제