Json 响应中的无法访问嵌套列表

Cant access nested list in Json response

提问人:fearghal O reilly 提问时间:7/12/2022 更新时间:7/12/2022 访问量:26

问:

我尝试使用 retrofit 从 json 响应访问某些列表项,但似乎无法使用 get 方法访问。ListQuotes 似乎说 null

@JsonPropertyOrder({
    "page",
    "last_page",
    "quotes"
})
public class ListQuoteResponse {

@JsonProperty("quotes")
private List<Quote> quotes = null;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

/**
 * No args constructor for use in serialization
 *
 */
public ListQuoteResponse() {
}


@JsonProperty("quotes")
public List<Quote> getQuotes() {
    return quotes;
}

@JsonProperty("quotes")
public void setQuotes(List<Quote> quotes) {
    this.quotes = quotes;
}

@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
    return this.additionalProperties;
}

@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
    this.additionalProperties.put(name, value);
}

}

但是当我打开 Quote 类时,我无法访问任何属性

public class Quote {

@JsonProperty("id")
private Integer id;
@JsonProperty("dialogue")
private Boolean dialogue;

@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

/**
 * No args constructor for use in serialization
 *
 */
public Quote() {
}


@JsonProperty("id")
public Integer getId() {
    return id;
}

@JsonProperty("id")
public void setId(Integer id) {
    this.id = id;
}

@JsonProperty("dialogue")
public Boolean getDialogue() {
    return dialogue;
}

@JsonProperty("dialogue")
public void setDialogue(Boolean dialogue) {
    this.dialogue = dialogue;
}

我试过使用 listQuoteBodyResponse.body().getQuotes(),但这只返回随机数,当我尝试直接使用 Quote 类进行响应时,例如 QuoteResponse.body.getDialogue() 它只是返回 null

数组 json retrofit retrofit2 嵌套列表

评论


答: 暂无答案