ClimaCell API 的 JSON 转换为 POJO

huangapple go评论114阅读模式
英文:

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:

[
   {
      &quot;temp&quot;:[
         {
            &quot;observation_time&quot;:&quot;2020-05-30T03:00:00Z&quot;,
            &quot;min&quot;:{
               &quot;value&quot;:13.49,
               &quot;units&quot;:&quot;C&quot;
            }
         },
         {
            &quot;observation_time&quot;:&quot;2020-05-29T14:00:00Z&quot;,
            &quot;max&quot;:{
               &quot;value&quot;:28.15,
               &quot;units&quot;:&quot;C&quot;
            }
         }
      ],
      &quot;precipitation&quot;:[
         {
            &quot;observation_time&quot;:&quot;2020-05-29T07:00:00Z&quot;,
            &quot;max&quot;:{
               &quot;value&quot;:0,
               &quot;units&quot;:&quot;mm/hr&quot;
            }
         }
      ],
      &quot;feels_like&quot;:[
         {
            &quot;observation_time&quot;:&quot;2020-05-30T03:00:00Z&quot;,
            &quot;min&quot;:{
               &quot;value&quot;:13.49,
               &quot;units&quot;:&quot;C&quot;
            }
         },
         {
            &quot;observation_time&quot;:&quot;2020-05-29T14:00:00Z&quot;,
            &quot;max&quot;:{
               &quot;value&quot;:26.8,
               &quot;units&quot;:&quot;C&quot;
            }
         }
      ],
      &quot;humidity&quot;:[
         {
            &quot;observation_time&quot;:&quot;2020-05-29T13:00:00Z&quot;,
            &quot;min&quot;:{
               &quot;value&quot;:18.1,
               &quot;units&quot;:&quot;%&quot;
            }
         },
         {
            &quot;observation_time&quot;:&quot;2020-05-30T03:00:00Z&quot;,
            &quot;max&quot;:{
               &quot;value&quot;:80.4,
               &quot;units&quot;:&quot;%&quot;
            }
         }
      ],
      &quot;baro_pressure&quot;:[
         {
            &quot;observation_time&quot;:&quot;2020-05-29T15:00:00Z&quot;,
            &quot;min&quot;:{
               &quot;value&quot;:998.21,
               &quot;units&quot;:&quot;hPa&quot;
            }
         },
         {
            &quot;observation_time&quot;:&quot;2020-05-29T07:00:00Z&quot;,
            &quot;max&quot;:{
               &quot;value&quot;:1001.36,
               &quot;units&quot;:&quot;hPa&quot;
            }
         }
      ],
      &quot;wind_speed&quot;:[
         {
            &quot;observation_time&quot;:&quot;2020-05-29T09:00:00Z&quot;,
            &quot;min&quot;:{
               &quot;value&quot;:3.1,
               &quot;units&quot;:&quot;m/s&quot;
            }
         },
         {
            &quot;observation_time&quot;:&quot;2020-05-29T16:00:00Z&quot;,
            &quot;max&quot;:{
               &quot;value&quot;:8.98,
               &quot;units&quot;:&quot;m/s&quot;
            }
         }
      ],
      &quot;visibility&quot;:[
         {
            &quot;observation_time&quot;:&quot;2020-05-29T08:00:00Z&quot;,
            &quot;min&quot;:{
               &quot;value&quot;:24.13,
               &quot;units&quot;:&quot;km&quot;
            }
         },
         {
            &quot;observation_time&quot;:&quot;2020-05-29T15:00:00Z&quot;,
            &quot;max&quot;:{
               &quot;value&quot;:24.14,
               &quot;units&quot;:&quot;km&quot;
            }
         }
      ],
      &quot;observation_time&quot;:{
         &quot;value&quot;:&quot;2020-05-29&quot;
      },
      &quot;lat&quot;:30,
      &quot;lon&quot;:30
   }
]

Then I created POJO for this json, which looks like this:

public class ClimaCell {
 @SerializedName(&quot;temp&quot;)
 @Expose
 private List&lt;Values&gt; temp = new ArrayList&lt;&gt;();

 @SerializedName(&quot;precipitation&quot;)
 @Expose
 private List&lt;Values&gt; precipitation = new ArrayList&lt;&gt;();

 @SerializedName(&quot;feels_like&quot;)
 @Expose
 private List&lt;Values&gt; feelsLike = new ArrayList&lt;&gt;();

 @SerializedName(&quot;humidity&quot;)
 @Expose
 private List&lt;Values&gt; humidity = new ArrayList&lt;&gt;();

 @SerializedName(&quot;baro_pressure&quot;)
 @Expose
 private List&lt;Values&gt; baroPressure = new ArrayList&lt;&gt;();

 @SerializedName(&quot;wind_speed&quot;)
 @Expose
 private List&lt;Values&gt; windSpeed = new ArrayList&lt;&gt;();

 @SerializedName(&quot;visibility&quot;)
 @Expose
 private List&lt;Values&gt; visibility = new ArrayList&lt;&gt;();

 @SerializedName(&quot;observation_time&quot;)
 @Expose
 private List&lt;ObservationTime&gt; observationTime = new ArrayList&lt;&gt;();

 @SerializedName(&quot;lat&quot;)
 @Expose
 private Double lat;

 @SerializedName(&quot;lon&quot;)
 @Expose
 private Double lon;
}

public class Values {
 @SerializedName(&quot;observation_time&quot;)
 @Expose
 private String observationTime;

 private LinkedHashMap&lt;String, LinkedHashMap&lt;String, String&gt;&gt; storage;

 @Override
 public String toString() {
    return new ToStringBuilder(this).append(&quot;observationTime&quot;, observationTime).append(&quot;storage&quot;, 
                                                                             storage).toString();
 }
}

public class ObservationTime {
 @SerializedName(&quot;value&quot;)
 @Expose
 private String value;

 @Override
 public String toString() {
    return new ToStringBuilder(this).append(&quot;value&quot;, value).toString();
 }
}

All classes with

 @Getter
 @Setter
 @NoArgsConstructor
 @AllArgsConstructor

After executing this code:

    Object quote = restTemplate.getForObject(url, Object.class);
    var list = (List&lt;ClimaCell&gt;) quote;

    System.out.println(list.get(0).getTemp().get(0).getStorage().get(&quot;min&quot;).get(&quot;value&quot;));

The variable list & quote looks like this

list = {ArrayList@7179}  size = 1
 0 = {LinkedHashMap@7947}  size = 10
  &quot;temp&quot; -&gt; {ArrayList@7955}  size = 2
   key = &quot;temp&quot;
   value = {ArrayList@7955}  size = 2
    0 = {LinkedHashMap@7957}  size = 2
     &quot;observation_time&quot; -&gt; &quot;2020-05-30T03:00:00Z&quot;
      key = &quot;observation_time&quot;
      value = &quot;2020-05-30T03:00:00Z&quot;
     &quot;min&quot; -&gt; {LinkedHashMap@7970}  size = 2
      key = &quot;min&quot;
      value = {LinkedHashMap@7970}  size = 2
    1 = {LinkedHashMap@7981}  size = 2
     &quot;observation_time&quot; -&gt; &quot;2020-05-29T13:00:00Z&quot;
      key = &quot;observation_time&quot;
      value = &quot;2020-05-29T13:00:00Z&quot;
     &quot;max&quot; -&gt; {LinkedHashMap@7994}  size = 2
      key = &quot;max&quot;
      value = {LinkedHashMap@7994}  size = 2
  &quot;precipitation&quot; -&gt; {ArrayList@8009}  size = 1
   key = &quot;precipitation&quot;
   value = {ArrayList@8009}  size = 1
    0 = {LinkedHashMap@8011}  size = 2
     &quot;observation_time&quot; -&gt; &quot;2020-05-29T23:00:00Z&quot;
      key = &quot;observation_time&quot;
      value = &quot;2020-05-29T23:00:00Z&quot;
     &quot;max&quot; -&gt; {LinkedHashMap@8023}  size = 2
      key = &quot;max&quot;
      value = {LinkedHashMap@8023}  size = 2
       &quot;value&quot; -&gt; {Double@8065} 0.0625
       &quot;units&quot; -&gt; &quot;mm/hr&quot;
  &quot;feels_like&quot; -&gt; {ArrayList@8068}  size = 2 //it looks like the above
  &quot;humidity&quot; -&gt; {ArrayList@8070}  size = 2  //same
  &quot;baro_pressure&quot; -&gt; {ArrayList@8072}  size = 2  //...
  &quot;wind_speed&quot; -&gt; {ArrayList@8074}  size = 2
  &quot;visibility&quot; -&gt; {ArrayList@8076}  size = 2
  &quot;observation_time&quot; -&gt; {LinkedHashMap@8077}  size = 1
   key = &quot;observation_time&quot;
   value = {LinkedHashMap@8077}  size = 1
    &quot;value&quot; -&gt; &quot;2020-05-29&quot;
  &quot;lat&quot; -&gt; {Double@8050} 52.237049 //until this
  &quot;lon&quot; -&gt; {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 
    &#39;bootstrap&#39;; com.weathernow.web.model.ClimaCell is in unnamed module of loader &#39;app&#39;)
         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 ClimaCell API 的 JSON 转换为 POJO

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

就是这样 ClimaCell API 的 JSON 转换为 POJO

英文:

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&lt;ClimaCell&gt; pojo = gson.fromJson(quote, new TypeToken&lt;List&lt;ClimaCell&gt;&gt;() {}.getType());

And that's all ClimaCell API 的 JSON 转换为 POJO

huangapple
  • 本文由 发表于 2020年5月29日 21:23:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/62087036.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定