我从WebAPI收到以下格式的json响应:
{
"Status": "Successful Request",
"ErrorCode": 200,
"ResultCount": 1,
"Trends": {
"39381": {
"user_distance_units": 1,
"total_sep": 0,
"max_power": 0,
"max_rpm": 0,
"max_heart_rate_bpm": 0,
"distance": 0,
"avg_power": 0,
"avg_rpm": 0,
"avg_hr": 0,
"calories": 0,
"classes": "0",
"max_avg_power": 0,
"max_avg_rpm": 0,
"max_avg_hr": 0
}
}
}
我正在尝试将其映射到POJO。我在使用杰克逊(Jackson)批注时遇到一些困难。 39381是客户端ID,出于某种原因,Web服务会为此提供响应。该客户端ID是动态的,并且在每次调用不同用户时都会更改。在这种情况下,我似乎无法使用Jackson批注@JsonProperty。映射变得更加复杂。由于我确实不需要客户端ID信息,因此我真的想跳过该信息,只需将数据映射到该Trends类:
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({ "user_distance_units", "total_sep", "max_power", "max_rpm", "max_heart_rate_bpm", "distance",
"avg_power", "avg_rpm", "avg_hr", "calories", "classes", "max_avg_power", "max_avg_rpm", "max_avg_hr" })
public class Trends
{
@JsonProperty("user_distance_units")
private Integer userDistanceUnits;
@JsonProperty("total_sep")
private Integer totalSep;
@JsonProperty("max_power")
private Integer maxPower;
@JsonProperty("max_rpm")
private Integer maxRPM;
@JsonProperty("max_heart_rate_bpm")
private Integer maxHeartRateBPM;
@JsonProperty("distance")
private Integer distance;
@JsonProperty("avg_power")
private Integer avgPower;
@JsonProperty("avg_rpm")
private Integer avgRPM;
@JsonProperty("avg_hr")
private Integer avgHr;
@JsonProperty("calories")
private Integer calories;
@JsonProperty("classes")
private Integer classes;
@JsonProperty("max_avg_power")
private Integer maxAvgPower;
@JsonProperty("max_avg_rpm")
private Integer maxAvgRPM;
@JsonProperty("max_avg_hr")
private Integer maxAvgHr;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
有没有办法做到这一点?
答案 0 :(得分:1)
趋势以键在ClientId上的地图的形式返回。 因此,您可以定义带有内部趋势图的顶级Response对象:
class Response {
@JsonProperty("Status")
private String status;
@JsonProperty("ErrorCode")
private int errorCode;
@JsonProperty("ResultCount")
private int resultCount;
@JsonProperty("Trends")
private Map<String, Trends> trends;
}
如果您不关心clientId,则可以添加一个getter,它只是尝试返回地图的第一个元素。