我正在编写 .json 文件,我想读取该文件,但问题是,当我尝试将整个文件作为字符串读取时,它会在每个字符之前和之后添加空格,并且只是因为额外的字符而无法读取 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."}]
但它给出了奇怪的响应,例如:[ {“description 1”:“T he .....
修剪多余的空格我提到了这个,但仍然没有工作: Java如何用字符串中的单个空格替换2个或更多空格并仅删除前导空格
我正在使用此代码
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();
“响应”字符串包含奇怪的响应
那么有人可以帮助我吗?
对于写入文件,我使用:
FileOutputStream fos = new FileOutputStream(mypath);
DataOutputStream dos = new DataOutputStream(fos);
dos.writeChars(response);