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;
    }


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM