如何使用android Gson获取此Json对象的值

时间:2016-05-16 22:59:17

标签: android json gson

在此链接中有天气JSON信息 http://api.wunderground.com/api/89ef283108b7ea38/hourly/q/CA/San_Francisco.json

我试图获取这些字段的值列表 yday,temp> metric,dewpoint> metric,condition

但是在根类中我不能将该类属性更多地序列化为同一个hourly_forecast这样的SerializeName

public class Hourly_forecast_Children {

    @SerializedName("hourly_forecast")
    public List<DewpointInfo> dinfo;

    @SerializedName("hourly_forecast")
    public List< TemoInfo> tinfo;


    @SerializedName("hourly_forecast")
    public List<WeatherData> winfo;



    @SerializedName("hourly_forecast")
    public List< FCTTIME > finfo;
}

我收到了这个错误

05-16 21:54:55.334 16529-16529/com.example.tofaa.gsondemo2 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.tofaa.gsondemo2, PID: 16529
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.tofaa.gsondemo2/com.example.tofaa.gsondemo2.MainActivity}: java.lang.IllegalArgumentException: class com.example.tofaa.gsondemo2.json.Hourly_forecast_Children declares multiple JSON fields named hourly_forecast
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.IllegalArgumentException: class com.example.tofaa.gsondemo2.json.Hourly_forecast_Children declares multiple JSON fields named hourly_forecast
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:166)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:96)
at com.google.gson.Gson.getAdapter(Gson.java:416)
at com.google.gson.Gson.fromJson(Gson.java:878)
at com.google.gson.Gson.fromJson(Gson.java:844)
at com.google.gson.Gson.fromJson(Gson.java:793)
at com.google.gson.Gson.fromJson(Gson.java:765)
at com.example.tofaa.gsondemo2.MainActivity.onCreate(MainActivity.java:29)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
at android.app.ActivityThread.access$800(ActivityThread.java:151) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:135) 
at android.app.ActivityThread.main(ActivityThread.java:5254) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 

所以我在视觉上尝试了所有人并且工作正常 我该怎么做才能让他们一起工作

1 个答案:

答案 0 :(得分:2)

您为每个变量使用相同的已分类名称"hourly_forecast"。因此,Gson在将"hours_forecast"解析为正确的变量时感到困惑。

根据API,您的Hourly_forecast_Children类应该是这样的。

/**
 * version : 0.1
 * termsofService : http://www.wunderground.com/weather/api/d/terms.html
 * features : {"hourly":1}
 */

private ResponseEntity response;
/**
 * FCTTIME : {"hour":"17","hour_padded":"17","min":"00","min_unpadded":"0","sec":"0","year":"2016","mon":"5","mon_padded":"05","mon_abbrev":"May","mday":"16","mday_padded":"16","yday":"136","isdst":"1","epoch":"1463443200","pretty":"5:00 PM PDT on May 16, 2016","civil":"5:00 PM","month_name":"May","month_name_abbrev":"May","weekday_name":"Monday","weekday_name_night":"Monday Night","weekday_name_abbrev":"Mon","weekday_name_unlang":"Monday","weekday_name_night_unlang":"Monday Night","ampm":"PM","tz":"","age":"","UTCDATE":""}
 * temp : {"english":"65","metric":"18"}
 * dewpoint : {"english":"53","metric":"12"}
 * condition : Clear
 * icon : clear
 * icon_url : http://icons.wxug.com/i/c/k/clear.gif
 * fctcode : 1
 * sky : 9
 * wspd : {"english":"16","metric":"26"}
 * wdir : {"dir":"W","degrees":"278"}
 * wx : Sunny
 * uvi : 3
 * humidity : 64
 * windchill : {"english":"-9999","metric":"-9999"}
 * heatindex : {"english":"-9999","metric":"-9999"}
 * feelslike : {"english":"65","metric":"18"}
 * qpf : {"english":"0.0","metric":"0"}
 * snow : {"english":"0.0","metric":"0"}
 * pop : 0
 * mslp : {"english":"29.9","metric":"1013"}
 */

private List<HourlyForecastEntity> hourly_forecast;

public ResponseEntity getResponse() {
    return response;
}

public void setResponse(ResponseEntity response) {
    this.response = response;
}

public List<HourlyForecastEntity> getHourly_forecast() {
    return hourly_forecast;
}

public void setHourly_forecast(List<HourlyForecastEntity> hourly_forecast) {
    this.hourly_forecast = hourly_forecast;
}

public static class ResponseEntity {
    private String version;
    private String termsofService;
    /**
     * hourly : 1
     */

    private FeaturesEntity features;

    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    public String getTermsofService() {
        return termsofService;
    }

    public void setTermsofService(String termsofService) {
        this.termsofService = termsofService;
    }

    public FeaturesEntity getFeatures() {
        return features;
    }

    public void setFeatures(FeaturesEntity features) {
        this.features = features;
    }

    public static class FeaturesEntity {
        private int hourly;

        public int getHourly() {
            return hourly;
        }

        public void setHourly(int hourly) {
            this.hourly = hourly;
        }
    }
}

public static class HourlyForecastEntity {
    /**
     * hour : 17
     * hour_padded : 17
     * min : 00
     * min_unpadded : 0
     * sec : 0
     * year : 2016
     * mon : 5
     * mon_padded : 05
     * mon_abbrev : May
     * mday : 16
     * mday_padded : 16
     * yday : 136
     * isdst : 1
     * epoch : 1463443200
     * pretty : 5:00 PM PDT on May 16, 2016
     * civil : 5:00 PM
     * month_name : May
     * month_name_abbrev : May
     * weekday_name : Monday
     * weekday_name_night : Monday Night
     * weekday_name_abbrev : Mon
     * weekday_name_unlang : Monday
     * weekday_name_night_unlang : Monday Night
     * ampm : PM
     * tz :
     * age :
     * UTCDATE :
     */

    private FCTTIMEEntity FCTTIME;
    /**
     * english : 65
     * metric : 18
     */

    private TempEntity temp;
    /**
     * english : 53
     * metric : 12
     */

    private DewpointEntity dewpoint;
    private String condition;
    private String icon;
    private String icon_url;
    private String fctcode;
    private String sky;
    /**
     * english : 16
     * metric : 26
     */

    private WspdEntity wspd;
    /**
     * dir : W
     * degrees : 278
     */

    private WdirEntity wdir;
    private String wx;
    private String uvi;
    private String humidity;
    /**
     * english : -9999
     * metric : -9999
     */

    private WindchillEntity windchill;
    /**
     * english : -9999
     * metric : -9999
     */

    private HeatindexEntity heatindex;
    /**
     * english : 65
     * metric : 18
     */

    private FeelslikeEntity feelslike;
    /**
     * english : 0.0
     * metric : 0
     */

    private QpfEntity qpf;
    /**
     * english : 0.0
     * metric : 0
     */

    private SnowEntity snow;
    private String pop;
    /**
     * english : 29.9
     * metric : 1013
     */

    private MslpEntity mslp;

    public FCTTIMEEntity getFCTTIME() {
        return FCTTIME;
    }

    public void setFCTTIME(FCTTIMEEntity FCTTIME) {
        this.FCTTIME = FCTTIME;
    }

    public TempEntity getTemp() {
        return temp;
    }

    public void setTemp(TempEntity temp) {
        this.temp = temp;
    }

    public DewpointEntity getDewpoint() {
        return dewpoint;
    }

    public void setDewpoint(DewpointEntity dewpoint) {
        this.dewpoint = dewpoint;
    }

    public String getCondition() {
        return condition;
    }

    public void setCondition(String condition) {
        this.condition = condition;
    }

    public String getIcon() {
        return icon;
    }

    public void setIcon(String icon) {
        this.icon = icon;
    }

    public String getIcon_url() {
        return icon_url;
    }

    public void setIcon_url(String icon_url) {
        this.icon_url = icon_url;
    }

    public String getFctcode() {
        return fctcode;
    }

    public void setFctcode(String fctcode) {
        this.fctcode = fctcode;
    }

    public String getSky() {
        return sky;
    }

    public void setSky(String sky) {
        this.sky = sky;
    }

    public WspdEntity getWspd() {
        return wspd;
    }

    public void setWspd(WspdEntity wspd) {
        this.wspd = wspd;
    }

    public WdirEntity getWdir() {
        return wdir;
    }

    public void setWdir(WdirEntity wdir) {
        this.wdir = wdir;
    }

    public String getWx() {
        return wx;
    }

    public void setWx(String wx) {
        this.wx = wx;
    }

    public String getUvi() {
        return uvi;
    }

    public void setUvi(String uvi) {
        this.uvi = uvi;
    }

    public String getHumidity() {
        return humidity;
    }

    public void setHumidity(String humidity) {
        this.humidity = humidity;
    }

    public WindchillEntity getWindchill() {
        return windchill;
    }

    public void setWindchill(WindchillEntity windchill) {
        this.windchill = windchill;
    }

    public HeatindexEntity getHeatindex() {
        return heatindex;
    }

    public void setHeatindex(HeatindexEntity heatindex) {
        this.heatindex = heatindex;
    }

    public FeelslikeEntity getFeelslike() {
        return feelslike;
    }

    public void setFeelslike(FeelslikeEntity feelslike) {
        this.feelslike = feelslike;
    }

    public QpfEntity getQpf() {
        return qpf;
    }

    public void setQpf(QpfEntity qpf) {
        this.qpf = qpf;
    }

    public SnowEntity getSnow() {
        return snow;
    }

    public void setSnow(SnowEntity snow) {
        this.snow = snow;
    }

    public String getPop() {
        return pop;
    }

    public void setPop(String pop) {
        this.pop = pop;
    }

    public MslpEntity getMslp() {
        return mslp;
    }

    public void setMslp(MslpEntity mslp) {
        this.mslp = mslp;
    }

    public static class FCTTIMEEntity {
        private String hour;
        private String hour_padded;
        private String min;
        private String min_unpadded;
        private String sec;
        private String year;
        private String mon;
        private String mon_padded;
        private String mon_abbrev;
        private String mday;
        private String mday_padded;
        private String yday;
        private String isdst;
        private String epoch;
        private String pretty;
        private String civil;
        private String month_name;
        private String month_name_abbrev;
        private String weekday_name;
        private String weekday_name_night;
        private String weekday_name_abbrev;
        private String weekday_name_unlang;
        private String weekday_name_night_unlang;
        private String ampm;
        private String tz;
        private String age;
        private String UTCDATE;

        public String getHour() {
            return hour;
        }

        public void setHour(String hour) {
            this.hour = hour;
        }

        public String getHour_padded() {
            return hour_padded;
        }

        public void setHour_padded(String hour_padded) {
            this.hour_padded = hour_padded;
        }

        public String getMin() {
            return min;
        }

        public void setMin(String min) {
            this.min = min;
        }

        public String getMin_unpadded() {
            return min_unpadded;
        }

        public void setMin_unpadded(String min_unpadded) {
            this.min_unpadded = min_unpadded;
        }

        public String getSec() {
            return sec;
        }

        public void setSec(String sec) {
            this.sec = sec;
        }

        public String getYear() {
            return year;
        }

        public void setYear(String year) {
            this.year = year;
        }

        public String getMon() {
            return mon;
        }

        public void setMon(String mon) {
            this.mon = mon;
        }

        public String getMon_padded() {
            return mon_padded;
        }

        public void setMon_padded(String mon_padded) {
            this.mon_padded = mon_padded;
        }

        public String getMon_abbrev() {
            return mon_abbrev;
        }

        public void setMon_abbrev(String mon_abbrev) {
            this.mon_abbrev = mon_abbrev;
        }

        public String getMday() {
            return mday;
        }

        public void setMday(String mday) {
            this.mday = mday;
        }

        public String getMday_padded() {
            return mday_padded;
        }

        public void setMday_padded(String mday_padded) {
            this.mday_padded = mday_padded;
        }

        public String getYday() {
            return yday;
        }

        public void setYday(String yday) {
            this.yday = yday;
        }

        public String getIsdst() {
            return isdst;
        }

        public void setIsdst(String isdst) {
            this.isdst = isdst;
        }

        public String getEpoch() {
            return epoch;
        }

        public void setEpoch(String epoch) {
            this.epoch = epoch;
        }

        public String getPretty() {
            return pretty;
        }

        public void setPretty(String pretty) {
            this.pretty = pretty;
        }

        public String getCivil() {
            return civil;
        }

        public void setCivil(String civil) {
            this.civil = civil;
        }

        public String getMonth_name() {
            return month_name;
        }

        public void setMonth_name(String month_name) {
            this.month_name = month_name;
        }

        public String getMonth_name_abbrev() {
            return month_name_abbrev;
        }

        public void setMonth_name_abbrev(String month_name_abbrev) {
            this.month_name_abbrev = month_name_abbrev;
        }

        public String getWeekday_name() {
            return weekday_name;
        }

        public void setWeekday_name(String weekday_name) {
            this.weekday_name = weekday_name;
        }

        public String getWeekday_name_night() {
            return weekday_name_night;
        }

        public void setWeekday_name_night(String weekday_name_night) {
            this.weekday_name_night = weekday_name_night;
        }

        public String getWeekday_name_abbrev() {
            return weekday_name_abbrev;
        }

        public void setWeekday_name_abbrev(String weekday_name_abbrev) {
            this.weekday_name_abbrev = weekday_name_abbrev;
        }

        public String getWeekday_name_unlang() {
            return weekday_name_unlang;
        }

        public void setWeekday_name_unlang(String weekday_name_unlang) {
            this.weekday_name_unlang = weekday_name_unlang;
        }

        public String getWeekday_name_night_unlang() {
            return weekday_name_night_unlang;
        }

        public void setWeekday_name_night_unlang(String weekday_name_night_unlang) {
            this.weekday_name_night_unlang = weekday_name_night_unlang;
        }

        public String getAmpm() {
            return ampm;
        }

        public void setAmpm(String ampm) {
            this.ampm = ampm;
        }

        public String getTz() {
            return tz;
        }

        public void setTz(String tz) {
            this.tz = tz;
        }

        public String getAge() {
            return age;
        }

        public void setAge(String age) {
            this.age = age;
        }

        public String getUTCDATE() {
            return UTCDATE;
        }

        public void setUTCDATE(String UTCDATE) {
            this.UTCDATE = UTCDATE;
        }
    }

    public static class TempEntity {
        private String english;
        private String metric;

        public String getEnglish() {
            return english;
        }

        public void setEnglish(String english) {
            this.english = english;
        }

        public String getMetric() {
            return metric;
        }

        public void setMetric(String metric) {
            this.metric = metric;
        }
    }

    public static class DewpointEntity {
        private String english;
        private String metric;

        public String getEnglish() {
            return english;
        }

        public void setEnglish(String english) {
            this.english = english;
        }

        public String getMetric() {
            return metric;
        }

        public void setMetric(String metric) {
            this.metric = metric;
        }
    }

    public static class WspdEntity {
        private String english;
        private String metric;

        public String getEnglish() {
            return english;
        }

        public void setEnglish(String english) {
            this.english = english;
        }

        public String getMetric() {
            return metric;
        }

        public void setMetric(String metric) {
            this.metric = metric;
        }
    }

    public static class WdirEntity {
        private String dir;
        private String degrees;

        public String getDir() {
            return dir;
        }

        public void setDir(String dir) {
            this.dir = dir;
        }

        public String getDegrees() {
            return degrees;
        }

        public void setDegrees(String degrees) {
            this.degrees = degrees;
        }
    }

    public static class WindchillEntity {
        private String english;
        private String metric;

        public String getEnglish() {
            return english;
        }

        public void setEnglish(String english) {
            this.english = english;
        }

        public String getMetric() {
            return metric;
        }

        public void setMetric(String metric) {
            this.metric = metric;
        }
    }

    public static class HeatindexEntity {
        private String english;
        private String metric;

        public String getEnglish() {
            return english;
        }

        public void setEnglish(String english) {
            this.english = english;
        }

        public String getMetric() {
            return metric;
        }

        public void setMetric(String metric) {
            this.metric = metric;
        }
    }

    public static class FeelslikeEntity {
        private String english;
        private String metric;

        public String getEnglish() {
            return english;
        }

        public void setEnglish(String english) {
            this.english = english;
        }

        public String getMetric() {
            return metric;
        }

        public void setMetric(String metric) {
            this.metric = metric;
        }
    }

    public static class QpfEntity {
        private String english;
        private String metric;

        public String getEnglish() {
            return english;
        }

        public void setEnglish(String english) {
            this.english = english;
        }

        public String getMetric() {
            return metric;
        }

        public void setMetric(String metric) {
            this.metric = metric;
        }
    }

    public static class SnowEntity {
        private String english;
        private String metric;

        public String getEnglish() {
            return english;
        }

        public void setEnglish(String english) {
            this.english = english;
        }

        public String getMetric() {
            return metric;
        }

        public void setMetric(String metric) {
            this.metric = metric;
        }
    }

    public static class MslpEntity {
        private String english;
        private String metric;

        public String getEnglish() {
            return english;
        }

        public void setEnglish(String english) {
            this.english = english;
        }

        public String getMetric() {
            return metric;
        }

        public void setMetric(String metric) {
            this.metric = metric;
        }
    }
}

我强烈建议在Android工作室中使用GSONFormat插件来生成用于GSon解析的样板代码。

希望这有帮助。

干杯,