Json 值是 null 而有些不是 GSON 解析

Json values are null while some not with GSON parsing

This was my first question here :) Edited after feedback.

Objective: 从天气中提取温度 API

问题: 使用 GSON 解析时温度和其他属性返回 null,即使其他属性不返回。

我使用 jsonschema2pojo 生成了 POJO classes,奇怪的是我可以使用 GSON 从中获取一些值,但不能从中获取其他值。

这是 json 响应:

{
  "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
  }
}

当前对象的class:

@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();
    }

}

当我通过 GSON 调用它们时,我几乎可以获得所有信息,除了温度和 localtimeEpoch。甚至 uv 也是可达的。但由于某种原因不是温度。我尝试将它们作为 Double、Float 和 String,none 工作。

[
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代码:

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());

注意:json 字符串是通过 API.

生成的

输出为:

null
Sunny
Netherlands

Edited for clarity:

tgdavies 回答有效!

当我生成 POJO 时,我没有意识到 select GSON 有一个选项。其中 returns 带有 @SerializedName 注释的属性。

@JsonProperty 根据 documentation.

不适用于 GSON 上的骆驼案例

我的属性之后:

@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;

现在可以了。 我只是不知道骆驼案例有时会成为一个问题。现在我知道了。 谢谢!