2013-01-17 283 views
0

我使用wunderground獲取我的Java Web應用程序的天氣信息。 經過一番搜索後,我發現Gson看起來像一個很好的json解析器。Gson預計BEGIN_OBJECT,但是STRING

但下面的一些例子,我似乎無法弄清楚如何跳過根元素。 這裏是一個預測JSON請求的例子:

{ 
    "forecast" : { 
     "simpleforecast" : { 
      "forecastday" : [ { 
       "avehumidity" : 61, 
       "avewind" : { 
        "degrees" : 342, 
        "dir" : "NNW", 
        "kph" : 11, 
        "mph" : 7 
       }, 
       "conditions" : "Clear", 
       "date" : { 
        "ampm" : "PM", 
        "day" : 17, 
        "epoch" : "1358478000", 
        "hour" : 21, 
        "isdst" : "0", 
        "min" : "00", 
        "month" : 1, 
        "monthname" : "January", 
        "pretty" : "9:00 PM CST on January 17, 2013", 
        "sec" : 0, 
        "tz_long" : "America/Chicago", 
        "tz_short" : "CST", 
        "weekday" : "Thursday", 
        "weekday_short" : "Thu", 
        "yday" : 16, 
        "year" : 2013 
       }, 
       "high" : { 
        "celsius" : "11", 
        "fahrenheit" : "52" 
       }, 
       "icon" : "clear", 
       "icon_url" : "http://icons-ak.wxug.com/i/c/k/clear.gif", 
       "low" : { 
        "celsius" : "-2", 
        "fahrenheit" : "28" 
       }, 
       "maxhumidity" : 76, 
       "maxwind" : { 
        "degrees" : 340, 
        "dir" : "NNW", 
        "kph" : 18, 
        "mph" : 11 
       }, 
       "minhumidity" : 38, 
       "period" : 1, 
       "pop" : 10, 
       "qpf_allday" : { 
        "in" : 0.0, 
        "mm" : 0.0 
       }, 
       "qpf_day" : { 
        "in" : 0.0, 
        "mm" : 0.0 
       }, 
       "qpf_night" : { 
        "in" : 0.0, 
        "mm" : 0.0 
       }, 
       "skyicon" : "mostlysunny", 
       "snow_allday" : { 
        "cm" : 0, 
        "in" : 0 
       }, 
       "snow_day" : { 
        "cm" : 0, 
        "in" : 0 
       }, 
       "snow_night" : { 
        "cm" : 0, 
        "in" : 0 
       } 
      }, 
      { 
       "avehumidity" : 59, 
       "avewind" : { 
        "degrees" : 172, 
        "dir" : "South", 
        "kph" : 6, 
        "mph" : 4 
       }, 
       "conditions" : "Clear", 
       "date" : { 
        "ampm" : "PM", 
        "day" : 18, 
        "epoch" : "1358564400", 
        "hour" : 21, 
        "isdst" : "0", 
        "min" : "00", 
        "month" : 1, 
        "monthname" : "January", 
        "pretty" : "9:00 PM CST on January 18, 2013", 
        "sec" : 0, 
        "tz_long" : "America/Chicago", 
        "tz_short" : "CST", 
        "weekday" : "Friday", 
        "weekday_short" : "Fri", 
        "yday" : 17, 
        "year" : 2013 
       }, 
       "high" : { 
        "celsius" : "10", 
        "fahrenheit" : "50" 
       }, 
       "icon" : "clear", 
       "icon_url" : "http://icons-ak.wxug.com/i/c/k/clear.gif", 
       "low" : { 
        "celsius" : "-1", 
        "fahrenheit" : "30" 
       }, 
       "maxhumidity" : 84, 
       "maxwind" : { 
        "degrees" : 170, 
        "dir" : "South", 
        "kph" : 10, 
        "mph" : 6 
       }, 
       "minhumidity" : 40, 
       "period" : 2, 
       "pop" : 0, 
       "qpf_allday" : { 
        "in" : 0.0, 
        "mm" : 0.0 
       }, .................. 

現在我只關心的部分是forecastday。所以我所做的是以下幾點:

package entity.json; 

import java.util.List; 

public class Data { 
    private List<Forecast> forecastday; 

    public List<Forecast> getForecastdays() { 
     return forecastday; 
    } 
} 

預測類:

package entity.json; 

public class Forecast { 
    private int avehumidity; 
    private Avewind avewind; 
    private String conditions; 
    private DateWeather date; 
    private CelciusHigh high; 
    private String icon_url; 
    private CelciusLow low; 
    // + constructor, getters, setters 

這裏是我的方法:

public void createWeatherForecast(){ 
     String text = ""; 
     try { 
      URL url = new URL(jsonUrl); 
      System.out.println(jsonUrl); 
      Scanner s = new Scanner(url.openStream()); 
      while(s.hasNextLine()){ 
       text += s.nextLine(); 
      } 
      Data weather = new Gson().fromJson(text, Data.class); 
      System.out.println(new Gson().toJson(weather)); 
      System.out.println(weather.getForecastdays().get(0).getAvewind()); 
     } catch (IOException ex) { 
      Logger.getLogger(WeatherController.class.getName()).log(Level.SEVERE, null, ex); 
     } 
    } 

現在我應該也可以側重於預測, simpleforecast?

回答

1

僅僅因爲你在JSON對象中的一部分「只關心」並不能使GSON能夠魔術般地提供;)

要麼你需要讓你的POJO相匹配的JSON對象,或者編寫一個自定義的反序列化器,丟棄你不感興趣的JSON對象的部分並返回你的Data類的一個實例。

+0

對不起,遲到的迴應,我完全忘了這個問題。正如你所建議的,我寫了自己的反序列化器。在同伴程序員的幫助下:D – VercauterenP

相關問題