當前位置:才華齋>設計>網頁設計>

Android讀取本地json檔案的方法

網頁設計 閱讀(1.13W)

本文例項講述了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( 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;}