I have a json response from a WebAPI in this format:
{
"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
}
}
}
I am trying to map it to a POJO. I am having some difficulty with this one using Jackson annotations. The 39381 is a client ID, for some reason the webservice supplies this with the response. That client ID is dynamic and will change with every call for a different user. I cant seem to use the Jackson annotation @JsonProperty in this instance. The mapping becomes more complex. Since i really do not need the Client ID information, I would really like to skip that and just map the data to this Trends class:
@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>();
Is there a way to do this?
The trends are returned as a map keyed on ClientId. So you may define a top level Response object with inner map of trends:
class Response {
@JsonProperty("Status")
private String status;
@JsonProperty("ErrorCode")
private int errorCode;
@JsonProperty("ResultCount")
private int resultCount;
@JsonProperty("Trends")
private Map<String, Trends> trends;
}
If you do not care about the the clientId you may add a getter that simply attempts to return the first element of the map.