0
点赞
收藏
分享

微信扫一扫

Android开发本地json文件转为bean

一、json文件放在assets目录下

如下图:

二、关键的json文件转为String

public class JsonDataUtil {

    public static String getJson(Context context,String fileName){
        StringBuilder stringBuilder = new StringBuilder();

        try {
            AssetManager assetManager = context.getAssets();
            BufferedReader bf = new BufferedReader(new InputStreamReader(
                    assetManager.open(fileName)
            ));
            String line;
            while ((line = bf.readLine()) != null){
                stringBuilder.append(line);
            }
        }catch (IOException e){
            e.printStackTrace();
        }
        return stringBuilder.toString();
    }
}

三、调用示例

String jsonStr = JsonDataUtil.getJson(this,"tblist.json");
    MsgTBListResult msgTBListResult = new Gson().fromJson(jsonStr,MsgTBListResult.class);
举报

相关推荐

0 条评论