2 回答

TA貢獻(xiàn)1851條經(jīng)驗(yàn) 獲得超3個(gè)贊
您需要使用根屬性model,
您可以重命名MyObject并MyModel創(chuàng)建一個(gè)MyObject
@JsonInclude(value = JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
public class MyObject{
@JsonProperty("model")
MyModel model;
}
然后檢查model

TA貢獻(xiàn)1827條經(jīng)驗(yàn) 獲得超4個(gè)贊
實(shí)際上,您需要 MyObject 中的兩個(gè)對象。
@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
}
現(xiàn)在,當(dāng)您像下面那樣使用它時(shí),它會(huì)正常工作。
ObjectMapper mapper = new ObjectMapper();
MyObject object = mapper.readValue(hit.getSourceAsString(), MyObject.class);
添加回答
舉報(bào)