2 回答

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

TA貢獻(xiàn)1827條經(jīng)驗 獲得超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
}
現(xiàn)在,當(dāng)您像下面那樣使用它時,它會正常工作。
ObjectMapper mapper = new ObjectMapper();
MyObject object = mapper.readValue(hit.getSourceAsString(), MyObject.class);
添加回答
舉報