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