1. 程式人生 > >Android-Android解析JSON

Android-Android解析JSON

public class ParseWeatherJson {   
    public WeatherBean parseWeatherjsonStream(InputStream in) throws IOException{
        JsonReader jsonReader = new JsonReader(new InputStreamReader(in));
        try {
            return readWeatherBean(jsonReader);
        } finally {
           jsonReader.close();
        }
    }
   
    private WeatherBean readWeatherBean(JsonReader jsonReader) throws IOException{
        Weatherinfo weatherinfo = null;
        jsonReader.beginObject();
        while(jsonReader.hasNext()){
            String name = jsonReader.nextName();
            if(name.equals("weatherinfo")){
                weatherinfo = readWeatherInfo(jsonReader);
            }else{
                jsonReader.skipValue();
            }
        }
        jsonReader.endObject();
        return new WeatherBean(weatherinfo);
    }
    private Weatherinfo readWeatherInfo(JsonReader jsonReader) throws IOException {
        String mcity = null;
        String mcityid = null;
        String mtemp1 = null;
        String mtemp2 = null;
        String mweather = null;
        String mptime = null;
       
        jsonReader.beginObject();
        while(jsonReader.hasNext()){
            String name = jsonReader.nextName();
            if(name.equals("city")){
                mcity = jsonReader.nextString();
            }else if(name.equals("cityid")){
                mcityid = jsonReader.nextString();
            }else if(name.equals("temp1")){
                mtemp1 = jsonReader.nextString();
            }else if(name.equals("temp2")){
                mtemp2 = jsonReader.nextString();
            }else if(name.equals("weather")){
                mweather = jsonReader.nextString();
            }else if(name.equals("ptime")){
                mptime = jsonReader.nextString();
            }else{
                jsonReader.skipValue();
            }
        }
        jsonReader.endObject();
       
        return new Weatherinfo(mcity, mcityid, mtemp1, mtemp2, mweather, mptime);
    }
}

public class WeatherBean {
    private Weatherinfo weatherinfo;
   
    public WeatherBean(Weatherinfo weatherinfo){
        this.weatherinfo = weatherinfo;
    }
   
    public Weatherinfo getWeatherInfo(){
        return weatherinfo;
    }
   
    public void setWeatherInfo(Weatherinfo info){
        this.weatherinfo = info;
    }
} 

public class Weatherinfo {
    private String mcity;
    private String mcityid;
    private String mtemp1;
    private String mtemp2;
    private String mweather;
    private String mptime;
   
    public Weatherinfo(String city, String cityId, String tempOne, String tempTwo,
            String weather, String time){
        this.mcity = city;
        this.mcityid = cityId;
        this.mtemp1 = tempOne;
        this.mtemp2 = tempTwo;
        this.mweather = weather;
        this.mptime = time;
    }
   
    public String getCity(){
        return mcity;
    }
   
    public void setCity(String city){
        this.mcity = city;
    }
   
    public String getCityId(){
        return mcityid;
    }
   
    public void setCityId(String cityId){
        this.mcityid = cityId;
    }
   
    public String getTempOne(){
        return mtemp1;
    }
   
    public void setTempOne(String temp){
        this.mtemp1 = temp;
    }
   
    public String getTempTwo(){
        return mtemp2;
    }
   
    public void setTempTwo(String temp){
        this.mtemp2 = temp;
    }
   
    public String getWeather(){
        return mweather;
    }
   
    public void setWeather(String weather){
        this.mweather = weather;
    }
   
    public String getTime(){
        return mptime;
    }
   
    public void setTime(String time){
        this.mptime = time;
    }
}