读取assets的json工具类


public class JsonFileReader { public static String getJson(Context context, String fileName) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        AssetManager assetManager = context.getAssets();
        InputStream inputStream = assetManager.open(fileName);
        BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
        byte[] buffer = new byte[1024];
        int len;
        while ((len = bufferedInputStream.read(buffer)) != -1) {
            baos.write(buffer, 0, len);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }finally {
        try {
            baos.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return baos.toString();
}
}

上方读出来是一个字符串至于解析就要大家自己写了

猜你喜欢

转载自blog.csdn.net/qq_42046338/article/details/83985314