码迷,mamicode.com
首页 > 移动开发 > 详细

Android 读取assets文件夹中json文件

时间:2017-05-31 12:14:49      阅读:305      评论:0      收藏:0      [点我收藏+]

标签:class   gets   public   parse   try   nts   state   ring   oid   

这里要介绍一下 读取assets文件夹中json文件 转换成list 集合

只接看代码 非常简单

public static List<State> getStates(Context context) {
        InputStream is = null;
        ByteArrayOutputStream bos = null;
        try {
            is = context.getAssets().open("area.json");
            bos = new ByteArrayOutputStream();
            byte[] bytes = new byte[4 * 1024];
            int len = 0;
            while ((len = is.read(bytes)) != -1) {
                bos.write(bytes, 0, len);
            }
            final String json = new String(bos.toByteArray());
            final Areas areas = JSON.parseObject(json, Areas.class);
            final List<State> states = areas.getStates();

            return states;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (is != null)
                    is.close();
                if (bos != null)
                    bos.close();
            } catch (IOException e) {
                Log.e(TAG, "getStates", e);
            }
        }
        return null;
    }

对应json 写好实体类 就OK了

Android 读取assets文件夹中json文件

标签:class   gets   public   parse   try   nts   state   ring   oid   

原文地址:http://www.cnblogs.com/teddy-yan/p/6922957.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!