首页 > 系统 > Android > 正文

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

2019-10-23 20:34:57
字体:
来源:转载
供稿:网友

本文实例讲述了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开发频道。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表