本文例項講述了Android讀取本地json檔案的方法。分享給大家供大家參考,具體如下:
1、讀取本地JSON ,但是顯示漢字亂碼
public static String readLocalJson(Context context, String fileName){ String jsonString=""; String resultString=""; try { BufferedReader bufferedReader=new BufferedReader(new InputStreamReader( esources()ssets()(fileName))); while ((jsonString=Line())!=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=esources()ssets()(fileName); byte[] buffer=new byte[lable()]; (buffer); resultString=new String(buffer,"GB2312"); } catch (Exception e) { // TODO: handle exception } return resultString;}