简体   繁体   English

以JAVA格式在JSON文件中写入数据

[英]Write data in JSON file in right form JAVA

I have question about the form of the data input in JSON. 我对JSON中的数据输入形式有疑问。

I save data with a button click in a json file, but the output is wrong. 我通过在json文件中单击按钮来保存数据,但输出错误。 How can I fix this problem, so that the new dataset is after the comma? 如何解决此问题,以便新数据集位于逗号之后?

JSONObject json = new JSONObject();
File filename = new File("dbsettings.json");

json.put("driver", textFieldDriver.getText());
json.put("url", textFieldURL.getText());
json.put("scheme", textFieldscheme.getText());          
json.put("name", textFieldDBname.getText());

try {
    System.out.println("Writting Data into JSONfile ...");
    System.out.println(json);
    FileWriter jsonFileWriter = new FileWriter(filename.getAbsoluteFile(), true);
    jsonFileWriter.write(json.toJSONString());
    jsonFileWriter.flush();
    jsonFileWriter.close();
    System.out.println("Done!");

} catch (IOException e) {
    e.printStackTrace();
}
JOptionPane.showMessageDialog(
    null, "Save Data successful", "Information", JOptionPane.INFORMATION_MESSAGE
);
setVisible(false);

this is my Output: 这是我的输出:

[{
    "driver": "oracle.jdbc.driver.OracleDriver",
    "url": "dburl1",
    "scheme": "myscheme1",
    "name": "mydbname1"
},{
    "driver": "oracle.jdbc.driver.OracleDriver",
    "url": "myurl",
    "scheme": "myscheme",
    "name": "mydbname"
}]{"scheme":"test3","name":"test4","driver":"test1","url":"test2"}

Pls help me! 请帮助我!

The problem is with your FileWriter object and having the true. 问题在于您的FileWriter对象并且具有true。 This means it will append to the file. 这意味着它将附加到文件中。 If you want to correctly add JSON objects to the file I would suggest reading all of the JSON objects form the file first. 如果要将JSON对象正确添加到文件中,我建议首先从文件中读取所有JSON对象。 Then store them in a list and append to the list any new entries from your program. 然后将它们存储在列表中,并将程序中的任何新条目附加到列表中。 When your program is done, loop the list of JSON objects collected and overwrite the file you are reading from with all the new and old JSON objects. 程序完成后,循环收集的JSON对象列表,并使用所有新旧JSON对象覆盖您正在读取的文件。

When program starts up, read from "dbsettings.json" and store all those JSON objects in some sort of data structure. 程序启动时,从“dbsettings.json”读取并将所有这些JSON对象存储在某种数据结构中。 An arraylist would work or perhaps a Hashmap if you need to find JSON objects later in your program based on some key. 如果您需要在程序中稍后根据某个键找到JSON对象,则可以使用arraylist或者也可以使用Hashmap。

Then as the program runs and you are receiving input from the user on new JSON objects just add them to your data collection. 然后,当程序运行并且您正在接收来自用户的新JSON对象的输入时,只需将它们添加到您的数据集合中。 Do not write them to the file every time you get a new one. 每次获得新文件时都不要将它们写入文件。 I recommend only overwriting the file with all the JSON objects in your collection when the user exits the program cleanly. 我建议当用户干净地退出程序时,只覆盖集合中所有JSON对象的文件。 That way you ensure that your data is in correct JSON form every time you start your program. 这样,您可以确保每次启动程序时数据都是正确的JSON格式。 The only downside is that if the program quits unexpectedly you lose all the data you entered during the run of the program. 唯一的缺点是,如果程序意外退出,您将丢失在程序运行期间输入的所有数据。

The other less optimal solution would be to do everything up above except you do this every time you get data from the user. 另一个不太理想的解决方案是完成上述所有操作,除非您每次从用户获取数据时执行此操作。

//for JSON you want to output them like "[json0, json1,json2,... etc]"
FileWriter jsonFileWriter = new FileWriter(filename.getAbsoluteFile());
jsonFileWriter.write("[")
for(int i = 0; i < jsonDataStructure.size(); i++)
{
    jsonFileWriter.write(jsonDataStructure.get(i).toJSONString());
    if(i + 1 != jsonDataStructure.size())
      jsonFileWriter.write(",");
}
jsonFileWriter.write("]");
jsonFileWriter.flush();
jsonFileWriter.close();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM