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

前端 未结 3 950
耶瑟儿~
耶瑟儿~ 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 05:54

    The current JSON response accessible via the link you provided seems to have a few design issues or suspicions for them. I'll post the JSON here in order not to let it be lost in the future:

    {
        "weather": {
            "forecast3hours": [
                {
                    "grid": {
                        "city": "충북",
                        "county": "충주시",
                        "village": "목행동",
                        "latitude": "37.0135600000",
                        "longitude": "127.9036500000"
                    },
                    "lightning1hour": "0",
                    "timeRelease": "2017-02-24 16:30:00",
                    "wind": {
                        "wspd2hour": "3.10",
                        "wdir1hour": "179.00",
                        "wspd1hour": "4.20",
                        "wdir2hour": "176.00",
                        "wdir3hour": "",
                        "wspd3hour": "",
                        "wdir4hour": "",
                        "wspd4hour": ""
                    },
                    "precipitation": {
                        "sinceOntime1hour": "0.00",
                        "type1hour": "0",
                        "sinceOntime2hour": "0.00",
                        "type2hour": "0",
                        "sinceOntime3hour": "",
                        "type3hour": "",
                        "sinceOntime4hour": "",
                        "type4hour": ""
                    },
                    "sky": {
                        "code1hour": "SKY_V01",
                        "name1hour": "맑음",
                        "code2hour": "SKY_V01",
                        "name2hour": "맑음",
                        "code3hour": "",
                        "name3hour": "",
                        "code4hour": "",
                        "name4hour": ""
                    },
                    "temperature": {
                        "temp1hour": "3.20",
                        "temp2hour": "2.00",
                        "temp3hour": "",
                        "temp4hour": ""
                    },
                    "humidity": {
                        "rh1hour": "41.00",
                        "rh2hour": "50.00",
                        "rh3hour": "",
                        "rh4hour": ""
                    },
                    "lightning2hour": "0",
                    "lightning3hour": "",
                    "lightning4hour": ""
                }
            ]
        },
        "common": {
            "alertYn": "N",
            "stormYn": "N"
        },
        "result": {
            "code": 9200,
            "requestUrl": "/weather/forecast/3hours?lon=127.9259&lat=36.991&version=1&appKey=4ce0462a-3884-30ab-ab13-93efb1bc171f",
            "message": "성공"
        }
    }
    

    From my point of view they are:

    • No arrays but artificially indexed object keys (and that's the subject of your question).
    • Empty strings for probably null values rather than nulls or just exclusion from the response.
    • Almost all values are expressed as string literals even if they seem to be non-strings.
    • Probably boolean values seem to be marked with the Yn suffix, and define true and false using "Y" and "N" respectively.

    This is why automatic POJO generators may be not the best way to deal with it because they may do not detect "real" type of a particular JSON string value, and moreover they cannot generate custom deserializers. Not sure why it was designed that way, but you can design your custom mappings to make it a little more programmatically-friendly with more control over them.

    final class Response {
    
        final Weather weather = null;
        final Common common = null;
        final Result result = null;
    
        @Override
        public String toString() {
            return new StringBuilder("Response{")
                    .append("weather=").append(weather)
                    .append(", common=").append(common)
                    .append(", result=").append(result)
                    .append('}').toString();
        }
    
    }
    
    final class Weather {
    
        final List<Forecast> forecast3hours = null;
    
        @Override
        public String toString() {
            return new StringBuilder("Weather{")
                    .append("forecast3hours=").append(forecast3hours)
                    .append('}').toString();
        }
    
    }
    
    final class Forecast {
    
        final Grid grid;
        final Date timeRelease;
        final List<Integer> lightnings;
        final List<Wind> winds;
        final List<Precipitation> precipitations;
        final List<Sky> skies;
        final List<Float> temperatures;
        final List<Float> humidities;
    
        Forecast(final Grid grid, final Date timeRelease, final List<Integer> lightnings, final List<Wind> winds, final List<Precipitation> precipitations,
                final List<Sky> skies, final List<Float> temperatures, final List<Float> humidities) {
            this.grid = grid;
            this.timeRelease = timeRelease;
            this.lightnings = lightnings;
            this.winds = winds;
            this.precipitations = precipitations;
            this.skies = skies;
            this.temperatures = temperatures;
            this.humidities = humidities;
        }
    
        @Override
        public String toString() {
            return new StringBuilder("Forecast{")
                    .append("grid=").append(grid)
                    .append(", timeRelease=").append(timeRelease)
                    .append(", lightnings=").append(lightnings)
                    .append(", winds=").append(winds)
                    .append(", precipitations=").append(precipitations)
                    .append(", skies=").append(skies)
                    .append(", temperatures=").append(temperatures)
                    .append(", humidities=").append(humidities)
                    .append('}').toString();
        }
    
    }
    
    final class Grid {
    
        final String city = null;
        final String county = null;
        final String village = null;
        final double latitude = Double.valueOf(0); // disable inlining the primitive double 0
        final double longitude = Double.valueOf(0); // disable inlining the primitive double 0
    
        @Override
        public String toString() {
            return new StringBuilder("Grid{")
                    .append("city='").append(city).append('\'')
                    .append(", county='").append(county).append('\'')
                    .append(", village='").append(village).append('\'')
                    .append(", latitude=").append(latitude)
                    .append(", longitude=").append(longitude)
                    .append('}').toString();
        }
    
    }
    
    final class Wind {
    
        final float speed;
        final float direction;
    
        Wind(final float speed, final float direction) {
            this.speed = speed;
            this.direction = direction;
        }
    
        @Override
        public String toString() {
            return new StringBuilder("Wind{")
                    .append("speed=").append(speed)
                    .append(", direction=").append(direction)
                    .append('}').toString();
        }
    
    }
    
    final class Precipitation {
    
        final float sinceOntime;
        final int type;
    
        Precipitation(final float sinceOntime, final int type) {
            this.sinceOntime = sinceOntime;
            this.type = type;
        }
    
        @Override
        public String toString() {
            return new StringBuilder("Precipitation{")
                    .append("sinceOntime='").append(sinceOntime).append('\'')
                    .append(", type=").append(type)
                    .append('}').toString();
        }
    
    }
    
    final class Sky {
    
        final String code;
        final String name;
    
        Sky(final String code, final String name) {
            this.code = code;
            this.name = name;
        }
    
        @Override
        public String toString() {
            return new StringBuilder("Sky{")
                    .append("code='").append(code).append('\'')
                    .append(", name='").append(name).append('\'')
                    .append('}').toString();
        }
    
    }
    
    final class Common {
    
        @SerializedName("alertYn")
        @JsonAdapter(YnToBooleanJsonDeserializer.class)
        final boolean alert = Boolean.valueOf(false); // disable inlining the primitive boolean false
    
        @SerializedName("stormYn")
        @JsonAdapter(YnToBooleanJsonDeserializer.class)
        final boolean storm = Boolean.valueOf(false); // disable inlining the primitive boolean false
    
        @Override
        public String toString() {
            return new StringBuilder("Common{")
                    .append("alert=").append(alert)
                    .append(", storm=").append(storm)
                    .append('}').toString();
        }
    
    }
    
    final class Result {
    
        final int code = Integer.valueOf(0); // disable inlining the primitive int 0
        final String requestUrl = null;
        final String message = null;
    
        @Override
        public String toString() {
            return new StringBuilder("Result{")
                    .append("code=").append(code)
                    .append(", requestUrl='").append(requestUrl).append('\'')
                    .append(", message='").append(message).append('\'')
                    .append('}').toString();
        }
    
    }
    

    Some of these mappings have explicit constructors - such objects must be instantiated manually in custom deserializers. If there is no constructors provided, then Gson can deal with such mapping itself having just enough information on how a particular object should be deserialized.

    Since that data should be parsed in a non-standard way, a couple of custom deserializators can be implemented. The following type adapter converts "Y" and "N" to true and false respectively.

    final class YnToBooleanJsonDeserializer
            implements JsonDeserializer<Boolean> {
    
        // Gson will instantiate this adapter itself
        private YnToBooleanJsonDeserializer() {
        }
    
        @Override
        public Boolean deserialize(final JsonElement jsonElement, final Type type, final JsonDeserializationContext context)
                throws JsonParseException {
            final String rawFlag = jsonElement.getAsString();
            switch ( rawFlag ) {
            case "N":
                return false;
            case "Y":
                return true;
            default:
                throw new JsonParseException("Can't parse: " + rawFlag);
            }
        }
    
    }
    

    The next JsonDeserializer tries to detect xxx<N>hour-like keys with regular expressions and extracting the <N> index building the lists required to construct a Forecast instance. Note that it can parse "lists" (the ones in the JSON) of arbitrary size.

    final class ForecastJsonDeserializer
            implements JsonDeserializer<Forecast> {
    
        // This deserializer does not hold any state and can be instantiated once per application life-cycle.
        private static final JsonDeserializer<Forecast> forecastJsonDeserializer = new ForecastJsonDeserializer();
    
        private ForecastJsonDeserializer() {
        }
    
        static JsonDeserializer<Forecast> getForecastJsonDeserializer() {
            return forecastJsonDeserializer;
        }
    
        @Override
        public Forecast deserialize(final JsonElement jsonElement, final Type type, final JsonDeserializationContext context)
                throws JsonParseException {
            final JsonObject jsonObject = jsonElement.getAsJsonObject();
            return new Forecast(
                    context.deserialize(jsonObject.get("grid"), Grid.class),
                    context.deserialize(jsonObject.get("timeRelease"), Date.class),
                    lightningsExtractor.parseList(jsonObject),
                    windsExtractor.parseList(jsonObject.get("wind").getAsJsonObject()),
                    precipitationsExtractor.parseList(jsonObject.get("precipitation").getAsJsonObject()),
                    skiesExtractor.parseList(jsonObject.get("sky").getAsJsonObject()),
                    temperaturesExtractor.parseList(jsonObject.get("temperature").getAsJsonObject()),
                    humiditiesExtractor.parseList(jsonObject.get("humidity").getAsJsonObject())
            );
        }
    
        private static final AbstractExtractor<Integer> lightningsExtractor = new AbstractExtractor<Integer>(compile("lightning(\\d)hour")) {
            @Override
            protected Integer parse(final int index, final JsonObject jsonObject) {
                final String rawLightning = jsonObject.get("lightning" + index + "hour").getAsString();
                if ( rawLightning.isEmpty() ) {
                    return null;
                }
                return parseInt(rawLightning);
            }
        };
    
        private static final AbstractExtractor<Wind> windsExtractor = new AbstractExtractor<Wind>(compile("(?:wdir|wspd)(\\d)hour")) {
            @Override
            protected Wind parse(final int index, final JsonObject jsonObject) {
                String rawSpeed = jsonObject.get("wspd" + index + "hour").getAsString();
                String rawDirection = jsonObject.get("wdir" + index + "hour").getAsString();
                if ( rawSpeed.isEmpty() && rawDirection.isEmpty() ) {
                    return null;
                }
                return new Wind(parseFloat(rawSpeed), parseFloat(rawDirection));
            }
        };
    
        private static final AbstractExtractor<Precipitation> precipitationsExtractor = new AbstractExtractor<Precipitation>(compile("(?:sinceOntime|type)(\\d)hour")) {
            @Override
            protected Precipitation parse(final int index, final JsonObject jsonObject) {
                final String rawSinceOntime = jsonObject.get("sinceOntime" + index + "hour").getAsString();
                final String rawType = jsonObject.get("type" + index + "hour").getAsString();
                if ( rawSinceOntime.isEmpty() && rawType.isEmpty() ) {
                    return null;
                }
                return new Precipitation(parseFloat(rawSinceOntime), parseInt(rawType));
            }
        };
    
        private static final AbstractExtractor<Sky> skiesExtractor = new AbstractExtractor<Sky>(compile("(?:code|name)(\\d)hour")) {
            @Override
            protected Sky parse(final int index, final JsonObject jsonObject) {
                final String rawCode = jsonObject.get("code" + index + "hour").getAsString();
                final String rawName = jsonObject.get("name" + index + "hour").getAsString();
                if ( rawCode.isEmpty() && rawName.isEmpty() ) {
                    return null;
                }
                return new Sky(rawCode, rawName);
            }
        };
    
        private static final AbstractExtractor<Float> temperaturesExtractor = new AbstractExtractor<Float>(compile("temp(\\d)hour")) {
            @Override
            protected Float parse(final int index, final JsonObject jsonObject) {
                final String rawTemperature = jsonObject.get("temp" + index + "hour").getAsString();
                if ( rawTemperature.isEmpty() ) {
                    return null;
                }
                return parseFloat(rawTemperature);
            }
        };
    
        private static final AbstractExtractor<Float> humiditiesExtractor = new AbstractExtractor<Float>(compile("rh(\\d)hour")) {
            @Override
            protected Float parse(final int index, final JsonObject jsonObject) {
                final String rawHumidity = jsonObject.get("rh" + index + "hour").getAsString();
                if ( rawHumidity.isEmpty() ) {
                    return null;
                }
                return parseFloat(rawHumidity);
            }
        };
    
        private abstract static class AbstractExtractor<T> {
    
            private final Pattern pattern;
    
            private AbstractExtractor(final Pattern pattern) {
                this.pattern = pattern;
            }
    
            protected abstract T parse(int index, JsonObject jsonObject);
    
            private List<T> parseList(final JsonObject jsonObject) {
                final List<T> list = new ArrayList<>();
                for ( final Entry<String, JsonElement> e : jsonObject.entrySet() ) {
                    final String key = e.getKey();
                    final Matcher matcher = pattern.matcher(key);
                    // Check if the given regular expression matches the key
                    if ( matcher.matches() ) {
                        // If yes, then just extract and parse the index 
                        final int index = parseInt(matcher.group(1));
                        // And check if there is enough room in the result list because the JSON response may contain unordered keys
                        while ( index > list.size() ) {
                            list.add(null);
                        }
                        // As Java lists are 0-based
                        if ( list.get(index - 1) == null ) {
                            // Assuming that null marks an object that's probably not parsed yet
                            list.set(index - 1, parse(index, jsonObject));
                        }
                    }
                }
                return list;
            }
    
        }
    
    }
    

    And now it all can be put together:

    public static void main(final String... args) {
        final Gson gson = new GsonBuilder()
                .setDateFormat("yyyy-MM-dd hh:mm:ss")
                .registerTypeAdapter(Forecast.class, getForecastJsonDeserializer())
                .create();
        final Response response = gson.fromJson(JSON, Response.class);
        System.out.println(response);
    }
    

    The output:

    Response{weather=Weather{forecast3hours=[Forecast{grid=Grid{city='충북', county='충주시', village='목행동', latitude=37.01356, longitude=127.90365}, timeRelease=Fri Feb 24 16:30:00 EET 2017, lightnings=[0, 0, null, null], winds=[Wind{speed=4.2, direction=179.0}, Wind{speed=3.1, direction=176.0}, null, null], precipitations=[Precipitation{sinceOntime='0.0', type=0}, Precipitation{sinceOntime='0.0', type=0}, null, null], skies=[Sky{code='SKY_V01', name='맑음'}, Sky{code='SKY_V01', name='맑음'}, null, null], temperatures=[3.2, 2.0, null, null], humidities=[41.0, 50.0, null, null]}]}, common=Common{alert=false, storm=false}, result=Result{code=9200, requestUrl='/weather/forecast/3hours?lon=127.9259&lat=36.991&version=1&appKey=4ce0462a-3884-30ab-ab13-93efb1bc171f', message='성공'}}

    0 讨论(0)
  • 2021-01-22 06:02

    It is not possible, Use following web site which create pojo of your json

    http://www.jsonschema2pojo.org/

    or their is plugin available Gson format for android studio

    0 讨论(0)
  • 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<Forecast3hour> forecast3hours = null;
    
            public List<Forecast3hour> getForecast3hours() {
                return forecast3hours;
            }
    
            public void setForecast3hours(List<Forecast3hour> 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();
    
    0 讨论(0)
提交回复
热议问题