Android读取本地json文件的方法(解决显示乱码问题)


本文实例讲述了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相关内容感兴趣的读者可查看本站专题:《Android操作json格式数据技巧总结》、《Android数据库操作技巧总结》、《Android编程之activity操作技巧总结》、《Android文件操作技巧汇总》、《Android编程开发之SD卡操作方法汇总》、《Android开发入门与进阶教程》、《Android资源操作技巧汇总》、《Android视图View技巧总结》及《Android控件用法总结》

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


« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3