首页 > 移动平台 > 详细

Android 读取assets文件夹中json文件

时间:2017-05-31 11:21:37      阅读:457      评论:0      收藏:0      [点我收藏+]

这里要介绍一下 读取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文件

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!