英文:
ClimaCell API json casting to POJO
问题
I created an application that takes weather data from the ClimaCell API and then displays it on the page. Below I will show how it looks. Firstly, Postman gives such results after sending the query:
[
{
"temp":[
{
"observation_time":"2020-05-30T03:00:00Z",
"min":{
"value":13.49,
"units":"C"
}
},
{
"observation_time":"2020-05-29T14:00:00Z",
"max":{
"value":28.15,
"units":"C"
}
}
],
...
// (other data omitted for brevity)
...
}
]
Then I created POJO for this JSON, which looks like this:
public class ClimaCell {
@SerializedName("temp")
@Expose
private List<Values> temp = new ArrayList<>();
@SerializedName("precipitation")
@Expose
private List<Values> precipitation = new ArrayList<>();
@SerializedName("feels_like")
@Expose
private List<Values> feelsLike = new ArrayList<>();
@SerializedName("humidity")
@Expose
private List<Values> humidity = new ArrayList<>();
@SerializedName("baro_pressure")
@Expose
private List<Values> baroPressure = new ArrayList<>();
@SerializedName("wind_speed")
@Expose
private List<Values> windSpeed = new ArrayList<>();
@SerializedName("visibility")
@Expose
private List<Values> visibility = new ArrayList<>();
@SerializedName("observation_time")
@Expose
private List<ObservationTime> observationTime = new ArrayList<>();
@SerializedName("lat")
@Expose
private Double lat;
@SerializedName("lon")
@Expose
private Double lon;
}
// (Other class definitions omitted for brevity)
After executing this code:
Object quote = restTemplate.getForObject(url, Object.class);
var list = (List<ClimaCell>) quote;
System.out.println(list.get(0).getTemp().get(0).getStorage().get("min").get("value"));
The variable list
and quote
looks like this:
list = {ArrayList@7179} size = 1
0 = {LinkedHashMap@7947} size = 10
"temp" -> {ArrayList@7955} size = 2
0 = {LinkedHashMap@7957} size = 2
"observation_time" -> "2020-05-30T03:00:00Z"
"min" -> {LinkedHashMap@7970} size = 2
1 = {LinkedHashMap@7981} size = 2
"observation_time" -> "2020-05-29T13:00:00Z"
"max" -> {LinkedHashMap@7994} size = 2
...
And console returns an error:
java.lang.ClassCastException: class java.util.LinkedHashMap cannot be cast to class
com.weathernow.web.model.ClimaCell
Could anyone help me with this? I've tried in many ways to cast this list to the ClimaCell class, but it never works.
英文:
I created an application that takes weather data from the ClimaCell API and then displays it on the page. Below I will show how it looks. Firstly Postman gives such results after sending the query:
[
{
"temp":[
{
"observation_time":"2020-05-30T03:00:00Z",
"min":{
"value":13.49,
"units":"C"
}
},
{
"observation_time":"2020-05-29T14:00:00Z",
"max":{
"value":28.15,
"units":"C"
}
}
],
"precipitation":[
{
"observation_time":"2020-05-29T07:00:00Z",
"max":{
"value":0,
"units":"mm/hr"
}
}
],
"feels_like":[
{
"observation_time":"2020-05-30T03:00:00Z",
"min":{
"value":13.49,
"units":"C"
}
},
{
"observation_time":"2020-05-29T14:00:00Z",
"max":{
"value":26.8,
"units":"C"
}
}
],
"humidity":[
{
"observation_time":"2020-05-29T13:00:00Z",
"min":{
"value":18.1,
"units":"%"
}
},
{
"observation_time":"2020-05-30T03:00:00Z",
"max":{
"value":80.4,
"units":"%"
}
}
],
"baro_pressure":[
{
"observation_time":"2020-05-29T15:00:00Z",
"min":{
"value":998.21,
"units":"hPa"
}
},
{
"observation_time":"2020-05-29T07:00:00Z",
"max":{
"value":1001.36,
"units":"hPa"
}
}
],
"wind_speed":[
{
"observation_time":"2020-05-29T09:00:00Z",
"min":{
"value":3.1,
"units":"m/s"
}
},
{
"observation_time":"2020-05-29T16:00:00Z",
"max":{
"value":8.98,
"units":"m/s"
}
}
],
"visibility":[
{
"observation_time":"2020-05-29T08:00:00Z",
"min":{
"value":24.13,
"units":"km"
}
},
{
"observation_time":"2020-05-29T15:00:00Z",
"max":{
"value":24.14,
"units":"km"
}
}
],
"observation_time":{
"value":"2020-05-29"
},
"lat":30,
"lon":30
}
]
Then I created POJO for this json, which looks like this:
public class ClimaCell {
@SerializedName("temp")
@Expose
private List<Values> temp = new ArrayList<>();
@SerializedName("precipitation")
@Expose
private List<Values> precipitation = new ArrayList<>();
@SerializedName("feels_like")
@Expose
private List<Values> feelsLike = new ArrayList<>();
@SerializedName("humidity")
@Expose
private List<Values> humidity = new ArrayList<>();
@SerializedName("baro_pressure")
@Expose
private List<Values> baroPressure = new ArrayList<>();
@SerializedName("wind_speed")
@Expose
private List<Values> windSpeed = new ArrayList<>();
@SerializedName("visibility")
@Expose
private List<Values> visibility = new ArrayList<>();
@SerializedName("observation_time")
@Expose
private List<ObservationTime> observationTime = new ArrayList<>();
@SerializedName("lat")
@Expose
private Double lat;
@SerializedName("lon")
@Expose
private Double lon;
}
public class Values {
@SerializedName("observation_time")
@Expose
private String observationTime;
private LinkedHashMap<String, LinkedHashMap<String, String>> storage;
@Override
public String toString() {
return new ToStringBuilder(this).append("observationTime", observationTime).append("storage",
storage).toString();
}
}
public class ObservationTime {
@SerializedName("value")
@Expose
private String value;
@Override
public String toString() {
return new ToStringBuilder(this).append("value", value).toString();
}
}
All classes with
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
After executing this code:
Object quote = restTemplate.getForObject(url, Object.class);
var list = (List<ClimaCell>) quote;
System.out.println(list.get(0).getTemp().get(0).getStorage().get("min").get("value"));
The variable list & quote looks like this
list = {ArrayList@7179} size = 1
0 = {LinkedHashMap@7947} size = 10
"temp" -> {ArrayList@7955} size = 2
key = "temp"
value = {ArrayList@7955} size = 2
0 = {LinkedHashMap@7957} size = 2
"observation_time" -> "2020-05-30T03:00:00Z"
key = "observation_time"
value = "2020-05-30T03:00:00Z"
"min" -> {LinkedHashMap@7970} size = 2
key = "min"
value = {LinkedHashMap@7970} size = 2
1 = {LinkedHashMap@7981} size = 2
"observation_time" -> "2020-05-29T13:00:00Z"
key = "observation_time"
value = "2020-05-29T13:00:00Z"
"max" -> {LinkedHashMap@7994} size = 2
key = "max"
value = {LinkedHashMap@7994} size = 2
"precipitation" -> {ArrayList@8009} size = 1
key = "precipitation"
value = {ArrayList@8009} size = 1
0 = {LinkedHashMap@8011} size = 2
"observation_time" -> "2020-05-29T23:00:00Z"
key = "observation_time"
value = "2020-05-29T23:00:00Z"
"max" -> {LinkedHashMap@8023} size = 2
key = "max"
value = {LinkedHashMap@8023} size = 2
"value" -> {Double@8065} 0.0625
"units" -> "mm/hr"
"feels_like" -> {ArrayList@8068} size = 2 //it looks like the above
"humidity" -> {ArrayList@8070} size = 2 //same
"baro_pressure" -> {ArrayList@8072} size = 2 //...
"wind_speed" -> {ArrayList@8074} size = 2
"visibility" -> {ArrayList@8076} size = 2
"observation_time" -> {LinkedHashMap@8077} size = 1
key = "observation_time"
value = {LinkedHashMap@8077} size = 1
"value" -> "2020-05-29"
"lat" -> {Double@8050} 52.237049 //until this
"lon" -> {Double@8079} 21.017532
and console returns such an error
java.lang.ClassCastException: class java.util.LinkedHashMap cannot be cast to class
com.weathernow.web.model.ClimaCell (java.util.LinkedHashMap is in module java.base of loader
'bootstrap'; com.weathernow.web.model.ClimaCell is in unnamed module of loader 'app')
at com.weathernow.web.Service.ClimaCellService.getDailyForecast(ClimaCellService.java:42) ~
[classes/:na]
at
com.weathernow.web.Controller.HomeController.getHomePageWithForecast(HomeController.java:21) ~
[classes/:na]
Could anyone help me with this? I've tried in many ways to cast this list on the ClimaCell class, but it never works
答案1
得分: 0
最终我解决了这个问题。首先,你需要创建一个良好的POJO类(对于第一次尝试的人来说,这可能是一个棘手的案例 - 这可能会有帮助),然后我使用了gson将JSON转换为我自己的类。
String quote = restTemplate.getForObject(url, String.class);
Gson gson = new Gson();
List<ClimaCell> pojo = gson.fromJson(quote, new TypeToken<List<ClimaCell>>() {}.getType());
就是这样
英文:
Finally I solved this problem. Firstly, you need to create a good POJO (it was a hard case for someone doing it for the first time - this may be helpful), then I used gson to cast JSON into my own class
String quote = restTemplate.getForObject(url, String.class);
Gson gson = new Gson();
List<ClimaCell> pojo = gson.fromJson(quote, new TypeToken<List<ClimaCell>>() {}.getType());
And that's all
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论