Open Weather Web GSON Parsing

Asked 1 years ago, Updated 1 years ago, 136 views

We're testing the open weather web to retrieve data using Retrofit2 By the way, I receive the data well, but there is a part of GSON parsing that I do not know, so I would like to ask you a question


{
  "coord": {
    "lon": 127,
    "lat": 37
  },
  "weather": [
    {
      "id": 801,
      "main": "Clouds",
      "description": "few clouds",
      "icon": "02d"
    }
  ],
  "base": "stations",
  "main": {
    "temp": 274.88,
    "pressure": 1017,
    "humidity": 26,
    "temp_min": 274.15,
    "temp_max": 276.15
  },
  "visibility": 11265,
  "wind": {
    "speed": 4.1,
    "deg": 320,
    "gust": 7.2
  },
  "clouds": {
    "all": 20
  },
  "dt": 1488862800,
  "sys": {
    "type": 1,
    "id": 7673,
    "message": 0.0139,
    "country": "KR",
    "sunrise": 1488837281,
    "sunset": 1488879111
  },
  "id": 1838431,
  "name": "Pyeong",
  "cod": 200
}

These are the data to be parsed


public class WatherData{
        @SerializedName("coord")
        Coord coprd;

        @SerializedName("weather")
        Weather wather;

        @SerializedName("main")
        Main main;

        public class Coord {
            @SerializedName("lon")String lon;
            @SerializedName("lat")String lat;

            public String getLon() {
                return lon;
            }

            public String getLat() {
                return lat;
            }

            @Override
            public String toString() {
                return "Coord{" +
                        "lon='" + lon + '\'' +
                        ", ", lat='" + lat + '\'' +
                        '}';
            }
        }

        public class Weather{
            @SerializedName("id") String id;
            @SerializedName("main") String main;
            @SerializedName("description") String description;
            @SerializedName("icon") String icon;

            @Override
            public String toString() {
                return "Wather{" +
                        "id='" + id + '\'' +
                        ", ", main='" + main + '\'' +
                        ", ", description='" + description + '\'' +
                        ", ", icon='" + icon + '\'' +
                        '}';
            }
        }

        public class Main{
            @SerializedName("temp") String temp;
            @SerializedName("pressure") String pressure;
            @SerializedName("humidity") String humidity;
            @SerializedName("temp_min") String temp_min;
            @SerializedName("temp_max") String temp_max;

            @Override
            public String toString() {
                return "Main{" +
                        "temp='" + temp + '\'' +
                        ", ", pressure='" + pressure + '\'' +
                        ", ", humidity='" + humidity + '\'' +
                        ", ", temp_min='" + temp_min + '\'' +
                        ", ", temp_max='" + temp_max + '\'' +
                        '}';
            }
        }

        @Override
        public String toString() {
            return "WatherData{" +
                    "coprd=" + coprd.toString() +
//                    //                    ", wather=" + wather.toString() +
                    ", ", main=" + main.toString() +
                    '}';
        }
    }

    ```



It's Percules 

 I want to parse only code, weather, and main data 

But among them, weather gets the data from the list, but I don't know how to parse it to GSON 

If the list data has a separate name, it can be parsed, but I don't know how to parse things that don't have a name at all 

gson retrofit2

2022-09-22 20:21

2 Answers

Hehehehe!)

I solved it

http://stackoverflow.com/questions/39361159/parsing-json-object-to-json-array-using-gson

This is how you do it...Stack of Flow is god god!!!!


2022-09-22 20:21

There may be people who do not know what to refer to because there is no answer adopted in the stack overflow,

I'm adding an article that I answered about GSON parsing.

On-json-data-transfer to https://hashcode.co.kr/questions/3559/ajax-


2022-09-22 20:21

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.