2 回答

TA贡献1851条经验 获得超3个赞
您需要使用根属性model,
您可以重命名MyObject并MyModel创建一个MyObject
@JsonInclude(value = JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
public class MyObject{
@JsonProperty("model")
MyModel model;
}
然后检查model

TA贡献1827条经验 获得超4个赞
实际上,您需要 MyObject 中的两个对象。
@JsonInclude(value = JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
public class MyModel {
@JsonProperty("id")
private String id;
@JsonProperty("serie")
private String serie;
//Generate getters and setters of these two
}
@JsonInclude(value = JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
public class ExternalObject {
@JsonProperty("serie")
private String serie;
@JsonProperty("fieldX")
private String fieldX;
//Generate getters and setters of these two
}
@JsonInclude(value = JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
public class MyObject{
@JsonProperty("model")
private MyModel model;
@JsonProperty("externalModel")
private ExternalObject externalModel;
//Generate getters and setters of these two
}
现在,当您像下面那样使用它时,它会正常工作。
ObjectMapper mapper = new ObjectMapper();
MyObject object = mapper.readValue(hit.getSourceAsString(), MyObject.class);
添加回答
举报