[英]Getting null values while deserialize json string to textview gson
[英]Json values are null while some not with GSON parsing
這是我在這里的第一個問題:)反饋后編輯。
目標:從天氣中提取溫度 API
問題:當使用 GSON 解析時,溫度和其他屬性返回 null,即使其他屬性沒有返回。
我使用jsonschema2pojo生成了 POJO 類,奇怪的是我可以使用 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
}
}
而當前 object 的 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。 甚至紫外線都可以到達。 但由於某種原因不是溫度。 我嘗試將它們作為 Double、Float 和 String,都沒有用。
[
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 生成的。
Output 是:
null
Sunny
Netherlands
為清楚起見編輯:
tgdavies 回答有效!
當我生成 POJO 時,我沒有意識到 select GSON 有一個選項。它返回帶有 @SerializedName 注釋的屬性。
根據文檔,@JsonProperty 不適用於 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;
現在可以了。 我只是不知道駱駝案例有時會成為一個問題。 現在我知道了。 謝謝!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.