This was my first question here :) Edited after feedback.
Objective: extract the temperature from the weather API
Problem: Temperatures and other properties are returning null when parsing with GSON even when others do not.
I generated the POJO classes using jsonschema2pojo and the weird thing is that I can get some values out of it with GSON, but not others.
This is the json response:
{
"location": {
"name": "Ommoord",
"region": "South Holland",
"country": "Netherlands",
"lat": XXX ,
"lon": XXX ,
"tz_id": "Europe/Amsterdam",
"localtime_epoch": 1647173864,
"localtime": "2022-03-13 13:17"
},
"current": {
"temp_c": 14.0,
"temp_f": 57.2,
"condition": {
"text": "Sunny"
},
"feelslike_c": 12.3,
"feelslike_f": 54.1,
"uv": 4.0
}
}
And the class for the Current object:
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"temp_c",
"temp_f",
"condition",
"feelslike_c",
"feelslike_f",
"uv"
})
@Generated("jsonschema2pojo")
public class Current {
@JsonProperty("temp_c")
@Getter
@Setter
@Expose
private Double tempC;
@JsonProperty("temp_f")
@Getter
@Setter
@Expose
private Double tempF;
@JsonProperty("condition")
@Getter
@Setter
@Expose
private Condition condition;
@JsonProperty("feelslike_c")
@Getter
@Setter
@Expose
private Double feelslikeC;
@JsonProperty("feelslike_f")
@Setter
@Getter
@Expose
private Double feelslikeF;
@JsonProperty("uv")
@Getter
@Setter
@Expose
private Double uv;
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(Current.class.getName()).append('@').append(Integer.toHexString(System.identityHashCode(this))).append('[');
sb.append("tempC");
sb.append('=');
sb.append(((this.tempC == null)?"<null>":this.tempC));
sb.append(',');
sb.append("tempF");
sb.append('=');
sb.append(((this.tempF == null)?"<null>":this.tempF));
sb.append(',');
sb.append("condition");
sb.append('=');
sb.append(((this.condition == null)?"<null>":this.condition));
sb.append(',');
sb.append("feelslikeC");
sb.append('=');
sb.append(((this.feelslikeC == null)?"<null>":this.feelslikeC));
sb.append(',');
sb.append("feelslikeF");
sb.append('=');
sb.append(((this.feelslikeF == null)?"<null>":this.feelslikeF));
sb.append(',');
sb.append("uv");
sb.append('=');
sb.append(((this.uv == null)?"<null>":this.uv));
sb.append(',');
if (sb.charAt((sb.length()- 1)) == ',') {
sb.setCharAt((sb.length()- 1), ']');
} else {
sb.append(']');
}
return sb.toString();
}
}
When I call them via GSON, I can get almost everything, except temperatures and localtimeEpoch. Even uv is reachable. But for some reason not the temperatures. I tried them as Double, Float and String, none work.
[
location=mypackage.utils.weather.Location@7d8995e
[
name=Ommoord,
region=South Holland,
country=Netherlands,
lat=xx.xx,lon=xx.xx,
tzId=<null>,
localtimeEpoch=<null>,
localtime=2022-03-13 13:17,
additionalProperties={}]
,current=mypackage.utils.weather.Current@130d63be
[
tempC=<null>,
tempF=<null>,
condition=mypackage.utils.weather.Condition@42a48628
[
text=Sunny,
additionalProperties={}
],
feelslikeC=<null>,
feelslikeF=<null>,
uv=4.0
]
,additionalProperties={}
]
Java code:
Gson gson= new Gson();
Weather weatherService = gson.fromJson(json, Weather.class);
System.out.println(weatherService.getCurrent().getTempC());
System.out.println(weatherService.getCurrent().getCondition().getText());
System.out.println(weatherService.getLocation().getCountry());
Note: The json string is generated via the API.
Output is:
null
Sunny
Netherlands