gpt4 book ai didi

java - 当我使用应用程序 json 查看器打开 JSON 文件时,它为空

转载 作者:太空宇宙 更新时间:2023-11-04 12:31:13 25 4
gpt4 key购买 nike

使用我的代码,当我使用智能手机应用程序 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。始终关闭 ReaderWriter 对象。

FileWriter file = new FileWriter(path+"filename31.json");
file.write(result);
file.close();

关于java - 当我使用应用程序 json 查看器打开 JSON 文件时,它为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37834447/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com