How to parse json object with gson based on dynamic SerializedName in Android

前端 未结 3 951
耶瑟儿~
耶瑟儿~ 2021-01-22 05:22

How can I parse this json objects with Gson this url: http://apis.skplanetx.com/weather/forecast/3hours?appKey=4ce0462a-3884-30ab-ab13-93efb1bc171f&version=1&lon=127.925

3条回答
  •  感情败类
    2021-01-22 06:16

    Use the Json scheme for the Gson parsing and it will give you the class like

    package com.ledge.bean;
    
    import com.google.gson.annotations.Expose;
    import com.google.gson.annotations.SerializedName;
    
    import java.util.List;
    
    public class WeatherParsing {
    
        @SerializedName("alertYn")
        @Expose
        private String alertYn;
        @SerializedName("stormYn")
        @Expose
        private String stormYn;
    
        public String getAlertYn() {
            return alertYn;
        }
    
        public void setAlertYn(String alertYn) {
            this.alertYn = alertYn;
        }
    
        public String getStormYn() {
            return stormYn;
        }
    
        public void setStormYn(String stormYn) {
            this.stormYn = stormYn;
        }
    
    
        public class Example {
    
            @SerializedName("weather")
            @Expose
            private WeatherParsing weather;
            @SerializedName("common")
            @Expose
            private WeatherParsing common;
            @SerializedName("result")
            @Expose
            private Result result;
    
            public WeatherParsing getWeather() {
                return weather;
            }
    
            public void setWeather(WeatherParsing weather) {
                this.weather = weather;
            }
    
            public WeatherParsing getCommon() {
                return common;
            }
    
            public void setCommon(WeatherParsing common) {
                this.common = common;
            }
    
            public Result getResult() {
                return result;
            }
    
            public void setResult(Result result) {
                this.result = result;
            }
    
        }
    
        public class Forecast3hour {
    
            @SerializedName("grid")
            @Expose
            private Grid grid;
            @SerializedName("wind")
            @Expose
            private Wind wind;
            @SerializedName("precipitation")
            @Expose
            private Precipitation precipitation;
            @SerializedName("sky")
            @Expose
            private Sky sky;
            @SerializedName("temperature")
            @Expose
            private Temperature temperature;
            @SerializedName("humidity")
            @Expose
            private Humidity humidity;
            @SerializedName("lightning1hour")
            @Expose
            private String lightning1hour;
            @SerializedName("timeRelease")
            @Expose
            private String timeRelease;
            @SerializedName("lightning2hour")
            @Expose
            private String lightning2hour;
            @SerializedName("lightning3hour")
            @Expose
            private String lightning3hour;
            @SerializedName("lightning4hour")
            @Expose
            private String lightning4hour;
    
            public Grid getGrid() {
                return grid;
            }
    
            public void setGrid(Grid grid) {
                this.grid = grid;
            }
    
            public Wind getWind() {
                return wind;
            }
    
            public void setWind(Wind wind) {
                this.wind = wind;
            }
    
            public Precipitation getPrecipitation() {
                return precipitation;
            }
    
            public void setPrecipitation(Precipitation precipitation) {
                this.precipitation = precipitation;
            }
    
            public Sky getSky() {
                return sky;
            }
    
            public void setSky(Sky sky) {
                this.sky = sky;
            }
    
            public Temperature getTemperature() {
                return temperature;
            }
    
            public void setTemperature(Temperature temperature) {
                this.temperature = temperature;
            }
    
            public Humidity getHumidity() {
                return humidity;
            }
    
            public void setHumidity(Humidity humidity) {
                this.humidity = humidity;
            }
    
            public String getLightning1hour() {
                return lightning1hour;
            }
    
            public void setLightning1hour(String lightning1hour) {
                this.lightning1hour = lightning1hour;
            }
    
            public String getTimeRelease() {
                return timeRelease;
            }
    
            public void setTimeRelease(String timeRelease) {
                this.timeRelease = timeRelease;
            }
    
            public String getLightning2hour() {
                return lightning2hour;
            }
    
            public void setLightning2hour(String lightning2hour) {
                this.lightning2hour = lightning2hour;
            }
    
            public String getLightning3hour() {
                return lightning3hour;
            }
    
            public void setLightning3hour(String lightning3hour) {
                this.lightning3hour = lightning3hour;
            }
    
            public String getLightning4hour() {
                return lightning4hour;
            }
    
            public void setLightning4hour(String lightning4hour) {
                this.lightning4hour = lightning4hour;
            }
    
        }
    
        public class Grid {
    
            @SerializedName("latitude")
            @Expose
            private String latitude;
            @SerializedName("longitude")
            @Expose
            private String longitude;
            @SerializedName("city")
            @Expose
            private String city;
            @SerializedName("county")
            @Expose
            private String county;
            @SerializedName("village")
            @Expose
            private String village;
    
            public String getLatitude() {
                return latitude;
            }
    
            public void setLatitude(String latitude) {
                this.latitude = latitude;
            }
    
            public String getLongitude() {
                return longitude;
            }
    
            public void setLongitude(String longitude) {
                this.longitude = longitude;
            }
    
            public String getCity() {
                return city;
            }
    
            public void setCity(String city) {
                this.city = city;
            }
    
            public String getCounty() {
                return county;
            }
    
            public void setCounty(String county) {
                this.county = county;
            }
    
            public String getVillage() {
                return village;
            }
    
            public void setVillage(String village) {
                this.village = village;
            }
    
        }
    
        public class Humidity {
    
            @SerializedName("rh1hour")
            @Expose
            private String rh1hour;
            @SerializedName("rh2hour")
            @Expose
            private String rh2hour;
            @SerializedName("rh3hour")
            @Expose
            private String rh3hour;
            @SerializedName("rh4hour")
            @Expose
            private String rh4hour;
    
            public String getRh1hour() {
                return rh1hour;
            }
    
            public void setRh1hour(String rh1hour) {
                this.rh1hour = rh1hour;
            }
    
            public String getRh2hour() {
                return rh2hour;
            }
    
            public void setRh2hour(String rh2hour) {
                this.rh2hour = rh2hour;
            }
    
            public String getRh3hour() {
                return rh3hour;
            }
    
            public void setRh3hour(String rh3hour) {
                this.rh3hour = rh3hour;
            }
    
            public String getRh4hour() {
                return rh4hour;
            }
    
            public void setRh4hour(String rh4hour) {
                this.rh4hour = rh4hour;
            }
        }
    
        public class Precipitation {
    
            @SerializedName("sinceOntime1hour")
            @Expose
            private String sinceOntime1hour;
            @SerializedName("type1hour")
            @Expose
            private String type1hour;
            @SerializedName("sinceOntime2hour")
            @Expose
            private String sinceOntime2hour;
            @SerializedName("type2hour")
            @Expose
            private String type2hour;
            @SerializedName("sinceOntime3hour")
            @Expose
            private String sinceOntime3hour;
            @SerializedName("type3hour")
            @Expose
            private String type3hour;
            @SerializedName("sinceOntime4hour")
            @Expose
            private String sinceOntime4hour;
            @SerializedName("type4hour")
            @Expose
            private String type4hour;
    
            public String getSinceOntime1hour() {
                return sinceOntime1hour;
            }
    
            public void setSinceOntime1hour(String sinceOntime1hour) {
                this.sinceOntime1hour = sinceOntime1hour;
            }
    
            public String getType1hour() {
                return type1hour;
            }
    
            public void setType1hour(String type1hour) {
                this.type1hour = type1hour;
            }
    
            public String getSinceOntime2hour() {
                return sinceOntime2hour;
            }
    
            public void setSinceOntime2hour(String sinceOntime2hour) {
                this.sinceOntime2hour = sinceOntime2hour;
            }
    
            public String getType2hour() {
                return type2hour;
            }
    
            public void setType2hour(String type2hour) {
                this.type2hour = type2hour;
            }
    
            public String getSinceOntime3hour() {
                return sinceOntime3hour;
            }
    
            public void setSinceOntime3hour(String sinceOntime3hour) {
                this.sinceOntime3hour = sinceOntime3hour;
            }
    
            public String getType3hour() {
                return type3hour;
            }
    
            public void setType3hour(String type3hour) {
                this.type3hour = type3hour;
            }
    
            public String getSinceOntime4hour() {
                return sinceOntime4hour;
            }
    
            public void setSinceOntime4hour(String sinceOntime4hour) {
                this.sinceOntime4hour = sinceOntime4hour;
            }
    
            public String getType4hour() {
                return type4hour;
            }
    
            public void setType4hour(String type4hour) {
                this.type4hour = type4hour;
            }
    
        }
    
        public class Result {
    
            @SerializedName("code")
            @Expose
            private Integer code;
            @SerializedName("requestUrl")
            @Expose
            private String requestUrl;
            @SerializedName("message")
            @Expose
            private String message;
    
            public Integer getCode() {
                return code;
            }
    
            public void setCode(Integer code) {
                this.code = code;
            }
    
            public String getRequestUrl() {
                return requestUrl;
            }
    
            public void setRequestUrl(String requestUrl) {
                this.requestUrl = requestUrl;
            }
    
            public String getMessage() {
                return message;
            }
    
            public void setMessage(String message) {
                this.message = message;
            }
    
        }
    
        public class Sky {
    
            @SerializedName("code1hour")
            @Expose
            private String code1hour;
            @SerializedName("name1hour")
            @Expose
            private String name1hour;
            @SerializedName("code2hour")
            @Expose
            private String code2hour;
            @SerializedName("name2hour")
            @Expose
            private String name2hour;
            @SerializedName("code3hour")
            @Expose
            private String code3hour;
            @SerializedName("name3hour")
            @Expose
            private String name3hour;
            @SerializedName("code4hour")
            @Expose
            private String code4hour;
            @SerializedName("name4hour")
            @Expose
            private String name4hour;
    
            public String getCode1hour() {
                return code1hour;
            }
    
            public void setCode1hour(String code1hour) {
                this.code1hour = code1hour;
            }
    
            public String getName1hour() {
                return name1hour;
            }
    
            public void setName1hour(String name1hour) {
                this.name1hour = name1hour;
            }
    
            public String getCode2hour() {
                return code2hour;
            }
    
            public void setCode2hour(String code2hour) {
                this.code2hour = code2hour;
            }
    
            public String getName2hour() {
                return name2hour;
            }
    
            public void setName2hour(String name2hour) {
                this.name2hour = name2hour;
            }
    
            public String getCode3hour() {
                return code3hour;
            }
    
            public void setCode3hour(String code3hour) {
                this.code3hour = code3hour;
            }
    
            public String getName3hour() {
                return name3hour;
            }
    
            public void setName3hour(String name3hour) {
                this.name3hour = name3hour;
            }
    
            public String getCode4hour() {
                return code4hour;
            }
    
            public void setCode4hour(String code4hour) {
                this.code4hour = code4hour;
            }
    
            public String getName4hour() {
                return name4hour;
            }
    
            public void setName4hour(String name4hour) {
                this.name4hour = name4hour;
            }
    
        }
    
        public class Temperature {
    
            @SerializedName("temp1hour")
            @Expose
            private String temp1hour;
            @SerializedName("temp2hour")
            @Expose
            private String temp2hour;
            @SerializedName("temp3hour")
            @Expose
            private String temp3hour;
            @SerializedName("temp4hour")
            @Expose
            private String temp4hour;
    
            public String getTemp1hour() {
                return temp1hour;
            }
    
            public void setTemp1hour(String temp1hour) {
                this.temp1hour = temp1hour;
            }
    
            public String getTemp2hour() {
                return temp2hour;
            }
    
            public void setTemp2hour(String temp2hour) {
                this.temp2hour = temp2hour;
            }
    
            public String getTemp3hour() {
                return temp3hour;
            }
    
            public void setTemp3hour(String temp3hour) {
                this.temp3hour = temp3hour;
            }
    
            public String getTemp4hour() {
                return temp4hour;
            }
    
            public void setTemp4hour(String temp4hour) {
                this.temp4hour = temp4hour;
            }
    
        }
    
        public class Weather {
    
            @SerializedName("forecast3hours")
            @Expose
            private List forecast3hours = null;
    
            public List getForecast3hours() {
                return forecast3hours;
            }
    
            public void setForecast3hours(List forecast3hours) {
                this.forecast3hours = forecast3hours;
            }
    
        }
    
        public class Wind {
    
            @SerializedName("wdir3hour")
            @Expose
            private String wdir3hour;
            @SerializedName("wspd3hour")
            @Expose
            private String wspd3hour;
            @SerializedName("wdir4hour")
            @Expose
            private String wdir4hour;
            @SerializedName("wspd4hour")
            @Expose
            private String wspd4hour;
            @SerializedName("wdir1hour")
            @Expose
            private String wdir1hour;
            @SerializedName("wspd1hour")
            @Expose
            private String wspd1hour;
            @SerializedName("wdir2hour")
            @Expose
            private String wdir2hour;
            @SerializedName("wspd2hour")
            @Expose
            private String wspd2hour;
    
            public String getWdir3hour() {
                return wdir3hour;
            }
    
            public void setWdir3hour(String wdir3hour) {
                this.wdir3hour = wdir3hour;
            }
    
            public String getWspd3hour() {
                return wspd3hour;
            }
    
            public void setWspd3hour(String wspd3hour) {
                this.wspd3hour = wspd3hour;
            }
    
            public String getWdir4hour() {
                return wdir4hour;
            }
    
            public void setWdir4hour(String wdir4hour) {
                this.wdir4hour = wdir4hour;
            }
    
            public String getWspd4hour() {
                return wspd4hour;
            }
    
            public void setWspd4hour(String wspd4hour) {
                this.wspd4hour = wspd4hour;
            }
    
            public String getWdir1hour() {
                return wdir1hour;
            }
    
            public void setWdir1hour(String wdir1hour) {
                this.wdir1hour = wdir1hour;
            }
    
            public String getWspd1hour() {
                return wspd1hour;
            }
    
            public void setWspd1hour(String wspd1hour) {
                this.wspd1hour = wspd1hour;
            }
    
            public String getWdir2hour() {
                return wdir2hour;
            }
    
            public void setWdir2hour(String wdir2hour) {
                this.wdir2hour = wdir2hour;
            }
    
            public String getWspd2hour() {
                return wspd2hour;
            }
    
            public void setWspd2hour(String wspd2hour) {
                this.wspd2hour = wspd2hour;
            }
    
        }
    }
    

    And in the code you can get the response are as follow:-

       WeatherParsing.Example getData = new Gson().fromJson(response,WeatherParsing.Example.class);
                    getData.getWeather().getForecast3hours().get(0).getLightning1hour();
                    getData.getCommon();
                    getData.getResult();
    

提交回复
热议问题