2017-02-24 15 views
2

動態SerializedName與GSON解析JSON對象如何可以解析此JSON對象與GSON這個網址: http://apis.skplanetx.com/weather/forecast/3hours?appKey=4ce0462a-3884-30ab-ab13-93efb1bc171f&version=1&lon=127.9259&lat=36.991如何基於Android中

"wind": { 
     "wdir3hour": "176.00", 
     "wspd3hour": "3.10", 
     "wdir4hour": "", 
     "wspd4hour": "", 
     "wdir1hour": "173.00", 
     "wspd1hour": "3.60", 
     "wdir2hour": "175.00", 
     "wspd2hour": "3.40" 
    }, 

    "precipitation": { 
     "sinceOntime1hour": "0.00", 
     "type1hour": "0", 
     "sinceOntime2hour": "0.00", 
     "type2hour": "0", 
     "sinceOntime3hour": "0.00", 
     "type3hour": "0", 
     "sinceOntime4hour": "", 
     "type4hour": "" 
    }, 

我怎麼能寫java模型此JSON對象。我不想寫每個這樣的提交 varibles。

class Wind{ 
@SerializedName("wdir1hour") 
private String wdir1hour; 
@SerializedName("wdir2hour") 
private String wdir2hour; 
@SerializedName("wdir3hour") 
private String wdir3hour; 
@SerializedName("wdir4hour") 
private String wdir4hour; 
} 

誰對這個json對象有更好的想法?

回答

2

通過您所提供的鏈接訪問當前JSON的反應似乎有一些設計問題或懷疑他們。我會在這裏發表JSON爲了不讓它失去了在未來:

{ 
    "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": "성공" 
    } 
} 

從我的角度來看,他們是:

  • 沒有數組,但是人爲索引對象鍵(這就是你的問題的主題)。
  • 可能爲null值的空字符串而不是null s或僅從響應中排除。
  • 幾乎所有的值都表示爲字符串文字,即使它們看起來不是字符串。
  • 大概布爾值似乎是標有Yn後綴,並且使用分別與"Y"限定"N"truefalse

這就是爲什麼自動POJO發電機可能無法對付它,因爲他們可能不檢測「真正的」類型的特定JSON字符串值的最佳方式,而且他們無法生成自定義解串器。不確定爲什麼它是這樣設計的,但是您可以設計自定義映射,使其更具編程友好性,並對其進行更多的控制。

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(); 
    } 

} 

一些映射的顯式的構造 - 這樣的對象必須手動自定義解串器被實例化。如果沒有提供構造函數,那麼Gson就可以自己處理這種映射,只需要有足夠的信息來說明如何對一個特定的對象進行反序列化。

由於應以非標準方式解析數據,因此可以實施一些自定義的反序列化器。以下類型的適配器分別將"Y""N"轉換爲truefalse

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); 
     } 
    } 

} 

JsonDeserializer嘗試檢測xxx<N>hour式的按鍵使用正則表達式,並提取<N>指數構建構建Forecast實例所需的列表。請注意,它可以解析任意大小的「列表」(JSON中的)。

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; 
     } 

    } 

} 

現在,這一切可以放在一起:

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); 
} 

輸出:

響應{天氣預報=天氣{forecast3hours = [預測{格=電網{城市=」 충북',county ='충주시',village ='목행동',緯度= 37.01356,經度= 127.90365},timeRelease = Fri Feb 24 16:30:00 EET 2017,lightnings = [0,0,null,null] = [風{速度= 4.2,方向= 179.0},風{速度= 3.1,方向= 176.0},空,空],沉澱= [沉澱ation {sinceOntime ='0.0',type = 0},降水{sinceOntime ='0.0',type = 0},null,null],skies = [Sky {code ='SKY_V01',name ='맑음'},Sky {code ='SKY_V01',name ='맑음'},null,null],溫度= [3.2,2.0,null,null],溼度= [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

@NizomjonHajiev沒問題,但還有一件事:我剛剛通過添加缺少的YnToBooleanJsonDeserializer實現(實際Y/N解析)我發佈原始答案時忘了提及,並將「country」更改爲「county」錯字。請參閱最新的答案。 –

+0

我再有問題我怎麼能registerType適配器GSON匕首java.lang.IllegalArgumentException異常:無法創建類com.example.nizomjon.enjoyweather.model.test.Response –

+0

提供 辛格爾頓 命名(生產) GSON轉換器provideGson(){ return new GsonBuilder() .setLenient() .create(); } 提供 辛格爾頓 命名(SKPLANET) GSON provideGsonSk(){ 返回新GsonBuilder() .setDateFormat( 「YYYY-MM-DD HH:MM:SS」) .registerTypeAdapter(Forecast.class,ForecastJsonDeserializer .getForecastJsonDeserializer()) .create(); } –

2

這是不可能的,使用下面的網站您的JSON其創建POJO

http://www.jsonschema2pojo.org/

或他們的插件提供GSON格式爲Android工作室

+0

它需要大量時間來填充回收站視圖。我需要一個更好的解決方案,如果Wind對象帶有1-12小時的結果,我必須改變agan.That是我需要動態的一個 –

+1

如果你確定它有12個字段最爲例子,那麼你可以使用12字段pojo來解析每一個json,你需要做的唯一的事情就是在訪問字段時做空檢查@NizomjonHajiev –

0

使用Json schemeGson解析,它會給你類如

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; 
     } 

    } 
} 

並在代碼,你可以得到的迴應如下: -

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