Android Gson解析Json

 public static final String json = "{\n" +
            "    \"error\": 0,\n" +
            "    \"status\": \"success\",\n" +
            "    \"date\": \"2014-05-10\",\n" +
            "    \"results\": [\n" +
            "        {\n" +
            "            \"currentCity\": \"南京\",\n" +
            "            \"weather_data\": [\n" +
            "                {\n" +
            "                    \"date\": \"周六(今天, 实时:19℃)\",\n" +
            "                    \"dayPictureUrl\": \"http://api.map.baidu.com/images/weather/day/dayu.png\",\n" +
            "                    \"nightPictureUrl\": \"http://api.map.baidu.com/images/weather/night/dayu.png\",\n" +
            "                    \"weather\": \"大雨\",\n" +
            "                    \"wind\": \"东南风5-6级\",\n" +
            "                    \"temperature\": \"18℃\"\n" +
            "                },\n" +
            "                {\n" +
            "                    \"date\": \"周日\",\n" +
            "                    \"dayPictureUrl\": \"http://api.map.baidu.com/images/weather/day/zhenyu.png\",\n" +
            "                    \"nightPictureUrl\": \"http://api.map.baidu.com/images/weather/night/duoyun.png\",\n" +
            "                    \"weather\": \"阵雨转多云\",\n" +
            "                    \"wind\": \"西北风4-5级\",\n" +
            "                    \"temperature\": \"21 ~ 14℃\"\n" +
            "                }\n" +
            "            ]\n" +
            "        }\n" +
            "    ]\n" +
            "}";


/**
 * 天气
 * Created by lezg on 2014/8/16.
 */
public final class Weather implements Serializable {

    private static final long serialVersionUID = 1L;

    private String date;
    private String dayPictureUrl;
    private String nightPictureUrl;
    private String weather;
    private String wind;
    private String temperature;

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public String getDayPictureUrl() {
        return dayPictureUrl;
    }

    public void setDayPictureUrl(String dayPictureUrl) {
        this.dayPictureUrl = dayPictureUrl;
    }

    public String getNightPictureUrl() {
        return nightPictureUrl;
    }

    public void setNightPictureUrl(String nightPictureUrl) {
        this.nightPictureUrl = nightPictureUrl;
    }

    public String getWeather() {
        return weather;
    }

    public void setWeather(String weather) {
        this.weather = weather;
    }

    public String getWind() {
        return wind;
    }

    public void setWind(String wind) {
        this.wind = wind;
    }

    public String getTemperature() {
        return temperature;
    }

    public void setTemperature(String temperature) {
        this.temperature = temperature;
    }

    @Override
    public String toString() {
        return "Weather [date=" + date + ", dayPictureUrl="
                + dayPictureUrl + ", nightPictureUrl="
                + nightPictureUrl + ", weather=" + weather
                + ", wind=" + wind + ", temperature=" + temperature
                + "]";
    }

}


public final class Results implements Serializable {

    private static final long serialVersionUID = 1L;

    private String currentCity;
    private List<Weather> weather_data;

    public String getCurrentCity() {
        return currentCity;
    }

    public void setCurrentCity(String currentCity) {
        this.currentCity = currentCity;
    }

    public List<Weather> getWeather_data() {
        return weather_data;
    }

    public void setWeather_data(List<Weather> weather_data) {
        this.weather_data = weather_data;
    }

    @Override
    public String toString() {
        return "Results [currentCity=" + currentCity + ", weather_data="
                + weather_data + "]";
    }

}


public final class Status implements Serializable {

    private static final long serialVersionUID = 1L;

    private String error;
    private String status;
    private String date;
    private List<Results> results;

    public String getError() {
        return error;
    }

    public void setError(String error) {
        this.error = error;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public List<Results> getResults() {
        return results;
    }

    public void setResults(List<Results> results) {
        this.results = results;
    }

    @Override
    public String toString() {
        return "Status [error=" + error + ", status=" + status
                + ", date=" + date + ", results=" + results + "]";
    }

}


 private void getJson() {
        try{
            Gson gson = new Gson();
            Status res = gson.fromJson(ParseJSON.json,Status.class);
            System.out.println("****"+res.getDate());
            System.out.println("----"+res.getResults().get(0).getWeather_data().get(0).getWeather());
        }catch (JsonSyntaxException e){
            e.printStackTrace();
        }
    }





你可能感兴趣的:(Android Gson解析Json)