Android菜鸟练习第五课 Gson解析

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

public class JsonBean {
    //1、内部嵌套的类必须是static的,要不然解析会出错;
    //2、类里面的属性名必须跟Json字段里面的Key是一模一样的;
    //3、内部嵌套的用[]括起来的部分是一个List,所以定义为 public List<B> b,而只用{}嵌套的就定义为 public C c
    public String error;
    public String status;
    public String date;
    public List<City> 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<City> getResults() {
        return results;
    }

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

    public static class City {
        public String currentCity;
        public 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_datas) {
            this.weather_data = weather_datas;
        }
    }

    public static class weather {
        public String date;
        public String dayPictureUrl;
        public String nightPictureUrl;
        public String weather;
        public String wind;
        public 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;
        }
    }
}
第三部分 Activity部分
public class MainActivity extends Activity {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Gson gson = new Gson();
        java.lang.reflect.Type type = new TypeToken<JsonBean>() {
        }.getType();
        try {
            //将json串转换为javaBean
            JsonBean jsonBean = gson.fromJson(getAccetsString(),type);
            } catch (JSONException e) {
                e.printStackTrace();
        }
    }


    //加载assets中的json文件
    public  String getAccetsString() throws JSONException {
        AssetManager assetManager = getAssets();
        String[] files = null;
        InputStream inputStream = null;
        try {
            inputStream = assetManager.open("json.txt");
        } catch (Exception e) {
            Log.e("tag", e.getMessage());
        }
        String result = readTextFile(inputStream);
        return result;
    }

    private String readTextFile(InputStream inputStream) {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        byte buf[] = new byte[1024];
        int len;
        try {
            while ((len = inputStream.read(buf)) != -1) {
                outputStream.write(buf, 0, len);
            }
            outputStream.close();
            inputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return outputStream.toString();

    }
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值