Android读取本地json文件的方法(解决显示乱码问题)
本文实例讲述了Android读取本地json文件的方法。分享给大家供大家参考,具体如下:
1、读取本地JSON,但是显示汉字乱码
publicstaticStringreadLocalJson(Contextcontext,StringfileName){
StringjsonString="";
StringresultString="";
try{
BufferedReaderbufferedReader=newBufferedReader(newInputStreamReader(
context.getResources().getAssets().open(fileName)));
while((jsonString=bufferedReader.readLine())!=null){
resultString+=jsonString;
}
}catch(Exceptione){
//TODO:handleexception
}
returnresultString;
}
2、读取本地JSON,显示汉字正确,txt文件设置时UTF-8,UNIX
publicstaticStringreadLocalJson(Contextcontext,StringfileName){
StringjsonString="";
StringresultString="";
try{
InputStreaminputStream=context.getResources().getAssets().open(fileName);
byte[]buffer=newbyte[inputStream.available()];
inputStream.read(buffer);
resultString=newString(buffer,"GB2312");
}catch(Exceptione){
//TODO:handleexception
}
returnresultString;
}
更多关于Android相关内容感兴趣的读者可查看本站专题:《Android操作json格式数据技巧总结》、《Android数据库操作技巧总结》、《Android编程之activity操作技巧总结》、《Android文件操作技巧汇总》、《Android编程开发之SD卡操作方法汇总》、《Android开发入门与进阶教程》、《Android资源操作技巧汇总》、《Android视图View技巧总结》及《Android控件用法总结》
希望本文所述对大家Android程序设计有所帮助。
热门推荐
3 祝福语简短的微信
10 幼儿祝福语大全简短贺卡
11 感谢教官文案祝福语简短
12 鼠年同事祝福语简短创意
13 元旦祝福语 简短独特群发
14 妈咪跟我生日祝福语简短
15 给儿子新婚祝福语简短
16 伴娘祝福语长文案简短
17 恭喜高考完祝福语简短
18 生日祝福语送老公简短