我的项目中有一个json文件如下图所示:

Android 读取Json文件的工具类

 读取json的工具类如下:

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字符串如下:

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

 

相关文章:

  • 2022-12-23
  • 2021-08-23
  • 2021-06-25
  • 2021-10-28
  • 2021-06-05
  • 2021-07-20
  • 2022-02-07
猜你喜欢
  • 2022-01-01
  • 2021-09-25
  • 2022-12-23
  • 2021-12-06
  • 2021-07-29
  • 2022-12-23
  • 2022-01-17
相关资源
相似解决方案