我得到的JSON-Response如下:
{
"status": "success",
"response": {
"entries": [
{
"id": 1,
"value": "test"
},
{
"id": 2,
"value": "test2"
}
]
}
}我想把它映射到像这样的对象上:
public class Response {
@JsonProperty("status")
private String status;
@JsonProperty("response.entries")
private Collection<ResponseEntry> entries;
}所以我正在寻找一种方法来给@JsonProperty一个路径,这样它就可以跳过"response“层。
发布于 2021-08-12 16:19:41
欢迎来到Stack Overflow。您可以为Collection<ResponseEntry>集合定义一个包装类,如下所示:
public class ResponseWrapper {
@JsonProperty("entries")
private Collection<ResponseEntry> entries;
}ResponseEntry类可以定义如下:
public class ResponseEntry {
@JsonProperty("id")
private int id;
@JsonProperty("value")
private String value;
}一旦定义了这些类,您就可以重写旧的Response类,如下所示:
public class Response {
@JsonProperty("status")
private String status;
@JsonProperty("response")
private ResponseWrapper responseWrapper;
}发布于 2021-08-12 15:05:42
您可以使用@JsonUnwrapped注释进行展平。
你可以像这样让你的类
public class Response {
private String status;
private Collection<ResponseEntry> entries;
}
public class ResponseEntry {
@JsonUnwrapped
private Entry entry;
}
pubic class Entry{
private Integer id;
private String value;
}https://stackoverflow.com/questions/68759602
复制相似问题