Java @jsonanygetter不';行不通

Java @jsonanygetter不';行不通,java,json,jackson,Java,Json,Jackson,假设我有以下类: @JsonIgnoreProperties(ignoreUnknown = true) @JsonInclude(JsonInclude.Include.NON_NULL) public class ObjectOfMonitoring { private BigInteger id; private Map<String, Object> properties = new HashMap<>(); @JsonAnySetter pub

假设我有以下类:

@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ObjectOfMonitoring {
  private BigInteger id;
  private Map<String, Object> properties = new HashMap<>();

  @JsonAnySetter
  public void add(String key, Object value) {
    properties.put(key, value);
  }

  @JsonAnyGetter
  public Map<String, Object> getProperties() {
    return properties;
  }
我想得到结果:

{"id":1,"key2":"value2","key1":"value1"}
但实际结果是:

{"id":1,"properties":{"key2":"value2","key1":"value1"}}

我做错了什么?以及如何获得预期结果?

确保
ObjectMapper
@JsonAnySetter
/
@JsonAnyGetter
注释来自相同的包

所有这些都应:

  • 可以从
    org.codehaus.jackson
    -哪个更老 杰克逊版本
  • 或者从
    com.fasterxml.jackson
    -哪个 较新(杰克逊在发行《杰克逊2》时已从Codehaus搬来)- (见)
如果您在项目中同时具有这两个依赖项,并且可以互换使用它们,那么您可能会遇到很难注意到的问题


最好的办法是从你的项目依赖关系中去掉
org.codehaus.jackson

可能会迟到,但考虑发布答案,因为它可能对将来的人有所帮助

这是您可以为类相应配置的示例代码

将读取JSON并与该类关联的主类:

public class Main {

    public static void main(String[] args) throws Exception {
        File jsonFile = new File("test.json").getAbsoluteFile();
        final ObjectMapper mapper = new ObjectMapper();
        MyObject myObject = mapper.readValue(jsonFile, MyPojo.class);
    }
}
您的自定义POJO:

class MyObject {

    private int id;
    private Map<String,String> customField;
    
    //Getter and Setter for POJO fields ommited

    @JsonAnySetter
    public void setCustomField(String key, Object value) {
        System.out.println(" Key : " + key + " Value : " + value);
        if(customField == null){
            customField = new HashMap<String,Object>();
        }
        
        customField.put(key,value);
    }
    
    @JsonAnyGetter
    public Map<String,String> getCustomField(){
        return customField;
    }
}
类MyObject{
私有int-id;
私有地图自定义域;
//POJO字段的Getter和Setter已指定
@JSONANYSETER
public void setCustomField(字符串键、对象值){
System.out.println(“键:+Key+”值:+Value”);
如果(customField==null){
customField=newhashmap();
}
customField.put(键、值);
}
@JsonAnyGetter
公共映射getCustomField(){
返回自定义字段;
}
}

如果您使用的是
ObjectMapper
中的
readValue
方法,那么即使对于JSON中的重复键,这也会起作用,但是如果您使用的是
ObjectMapper
中的
treeToValue
方法,那么对于重复键,这将失败,并且对于重复字段,您将只有最后一个值。在使用
treeToValue
方法时,我仍在想办法访问重复字段

你可能重复的问题只是继续问同一个问题,忽略回答前一个问题的努力?@sharonbn对不起,我已经解释了为什么我创建了这个问题(事实上,是错误的)。但是我仍然不明白为什么这个解决方案(使用@jsonanygetter)不起作用。这次我会让其他人花时间和精力来调查这个问题。
class MyObject {

    private int id;
    private Map<String,String> customField;
    
    //Getter and Setter for POJO fields ommited

    @JsonAnySetter
    public void setCustomField(String key, Object value) {
        System.out.println(" Key : " + key + " Value : " + value);
        if(customField == null){
            customField = new HashMap<String,Object>();
        }
        
        customField.put(key,value);
    }
    
    @JsonAnyGetter
    public Map<String,String> getCustomField(){
        return customField;
    }
}