-2

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

Marisabel
  • 11
  • 4
  • 2
    Share your `Weather` class definition. Sounds like field name does not match property name in json. – Chaosfire Mar 13 '22 at 13:10
  • However you are doing the mapping is not coping with `_` in names. – tgdavies Mar 13 '22 at 14:25
  • See https://stackoverflow.com/questions/2370745/convert-json-style-properties-names-to-java-camelcase-names-with-gson – tgdavies Mar 13 '22 at 14:26
  • Thank you so much ! I didn't know there could be an issue with those names. I'll see if I can fix that, at least I have something to work with now instead of feeling stuck. – Marisabel Mar 13 '22 at 20:13

1 Answers1

-2

Edited for clarity:

tgdavies answer worked!

When I generated the POJO, I did not realized there was an option to select GSON. Which returns the properties with the @SerializedName annotation.

@JsonProperty does not work with camel cases on GSON as per documentation.

My properties after:

@Generated("jsonschema2pojo")
public class Current {

    @SerializedName("temp_c")
    @Getter
    @Setter
    @Expose
    private Double tempC;
    @SerializedName("temp_f")
    @Getter
    @Setter
    @Expose
    private Double tempF;
    @JsonProperty("condition")
    @Getter
    @Setter
    @Expose
    private Condition condition;
    @SerializedName("feelslike_c")
    @Getter
    @Setter
    @Expose
    private Double feelslikeC;
    @SerializedName("feelslike_f")
    @Setter
    @Getter
    @Expose
    private Double feelslikeF;
    @JsonProperty("uv")
    @Getter
    @Setter
    @Expose
    private Double uv;

Now it works. I just did not know camel cases could be sometimes a problem. Now I do. Thank you!

Marisabel
  • 11
  • 4
  • 1
    Just to clarify - `@JsonProperty` is not standard, it is used when serializing/deserializing with `ObjectMapper`. `@SerializedName` is the equivalent when working with `Gson`. – Chaosfire Mar 14 '22 at 08:51
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 14 '22 at 13:19