本文实例讲述了Android读取本地json文件的方法。分享给大家供大家参考,具体如下:

1、读取本地JSON ,但是显示汉字乱码

public static String readLocalJson(Context context, String fileName){

String jsonString="";

String resultString="";

try {

BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(

context.getResources().getAssets().open(fileName)));

while ((jsonString=bufferedReader.readLine())!=null) {

resultString+=jsonString;

}

} catch (Exception e) {

// TODO: handle exception

}

return resultString;

}

2、读取本地JSON,显示汉字正确,txt文件设置时UTF-8,UNIX

public static String readLocalJson(Context context, String fileName){

String jsonString="";

String resultString="";

try {

InputStream inputStream=context.getResources().getAssets().open(fileName);

byte[] buffer=new byte[inputStream.available()];

inputStream.read(buffer);

resultString=new String(buffer,"GB2312");

} catch (Exception e) {

// TODO: handle exception

}

return resultString;

}

希望本文所述对大家Android程序设计有所帮助。

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐