编写.json文件并在Android中读取该文件 [英] writing .json file and read that file in Android

查看:184
本文介绍了编写.json文件并在Android中读取该文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写.json文件,但我想读取该文件, 但是问题是,当我尝试将整个文件读取为字符串时,它会在每个字符前后添加空格,并且由于多余的字符而无法读取json.

i m writing .json file and i want to read that file, but the problem is, when i try to read whole file as string it adds the space before and after every character and just because of extra chars it couldn't read json.

Json格式为

[{"description1":"The ThinkerA bronze sculpture by Auguste Rodin. It depicts a man in sober\nmeditation battling with a powerful internal struggle.","description2":"Steve JobsFounder of Apple, he is widely recognized as a charismatic pioneer of\nthe personal computer revolution.","description3":"Justin BieberBorn in 1994, the latest sensation in music industry with numerous\nawards in recent years."}]

但是它给出了奇怪的响应,例如: [{"d e s c r i p t i o n 1":"T h e .....

but it gives weired response like: [ { " d e s c r i p t i o n 1 " : " T h e .....

修剪多余的空间我指的是这个,但是stil没用: Java如何用字符串中的单个空格替换2个或更多空格并仅删除前导空格

to trim extra spaces i refered to this, but stil didnt work: Java how to replace 2 or more spaces with single space in string and delete leading spaces only

我正在使用此代码

File folderPath = Environment.getExternalStorageDirectory();
File mypath=new File(folderPath, "description.json");
StringBuffer fileData = new StringBuffer(1000);
BufferedReader reader = null;
reader = new BufferedReader(new FileReader(mypath));
char[] buf = new char[1024];
int numRead=0;

while((numRead=reader.read(buf)) != -1)
{
    String readData = String.valueOf(buf, 0, numRead);
    fileData.append(readData);
    buf = new char[1024];
}
String response = fileData.toString();

响应"字符串包含奇怪的响应

the "response" string contains weird response

那么有人可以帮助我吗?

so can anyone help me ?

用于写入文件,我使用:

for writing into file ,i m using :

FileOutputStream fos = new FileOutputStream(mypath);
DataOutputStream dos = new DataOutputStream(fos);
dos.writeChars(response);

推荐答案

writeChars将每个字符写为两个字节.

writeChars writes each character as two bytes.

http: //docs.oracle.com/javase/6/docs/api/java/io/DataOutputStream.html#writeChars(java.lang.String )

http://docs. oracle.com/javase/6/docs/api/java/io/DataOutputStream.html#writeChar(int )

Writes a char to the underlying output stream as a 2-byte value, high byte first. If no exception is thrown, the counter written is incremented by 2.

这篇关于编写.json文件并在Android中读取该文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆