使用我的代码,当我使用智能手机应用程序 JSON VIEWER
打开 JSON 文件
时,该文件为空。我看不到内容。
为什么?
现在,我将向您展示我的代码如何将文件保存在 json
先谢谢大家了!
public String showResult(View v) throws IOException {
String result = "";
JsonObject json = new JsonObject();
for (Planet p : plAdapter.getBox()) {
if (p.isSelected()){
json.put("name",p.getName());
json.put("distance",p.getDistance());
json.put("quantity",p.getQuantità());
}
}
result=json.toString();
String path = Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/droidText/";
//FileWriter file=null;
/* try {
file = new FileWriter(path+"filename5.json");
file.write(result);
}catch(IOException ie){}
finally{
file.flush();
file.close();
}*/
FileWriter file = new FileWriter(path+"filename31.json");
file.write(result);
file.close();
System.out.println("Successfully wrote Json result to file.");
/*catch(IOException r){
}*/
System.out.println(result);
return result;
}
JsonObject 类:
private static class JsonObject {
public void put(String name, String name1) {
}
}
最佳答案
您从未关闭过FileWriter
。始终关闭 Reader
和 Writer
对象。
FileWriter file = new FileWriter(path+"filename31.json");
file.write(result);
file.close();
关于java - 当我使用应用程序 json 查看器打开 JSON 文件时,它为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37834447/