Mapping JSON with varying object name

前端 未结 2 1600
余生分开走
余生分开走 2020-12-11 11:22

I\'m quite new to JSON, and I\'ve looked around trying to work out what to do but not sure if I fully understand. I am making an external API call returning:



        
2条回答
  •  独厮守ぢ
    2020-12-11 11:42

    you can use following mentioned annotation @JsonAnyGetter And @JsonAnySetter. Add this code into ur domain class. So any non-mapped attribute will get populated into "nonMappedAttributes" map while serializing and deserializing the Object.

    @JsonIgnore
    protected Map nonMappedAttributes;
    
    @JsonAnyGetter
    public Map getNonMappedAttributes() {
        return nonMappedAttributes;
    }
    
    @JsonAnySetter
    public void setNonMappedAttributes(String key, Object value) {
        if (nonMappedAttributes == null) {
            nonMappedAttributes = new HashMap();
        }
        if (key != null) {
            if (value != null) {
                nonMappedAttributes.put(key, value);
            } else {
                nonMappedAttributes.remove(key);
            }
        }
    
    }
    

提交回复
热议问题