Android读取Json文件的工具类

读取assets目录下json文件

 

读取json的工具类

import android.content.Context;
import android.content.res.AssetManager;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class GetJsonDataUtil {
    public 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();
    }
}

调用json工具类读取json文件生成json字符串

final String JsonData = new GetJsonDataUtil().getJson(this.getContext(), "appConfig.json");//获取assets目录下的json文件数据

 

posted @ 2021-03-31 19:43  Ring_1992  阅读(915)  评论(0编辑  收藏  举报