以正确的格式将数据写入JSON文件中 [英] Write data in JSON file in right form JAVA

查看:477
本文介绍了以正确的格式将数据写入JSON文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对JSON中数据输入的形式有疑问.

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. 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);

这是我的输出:

[{
    "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"}

请帮助我!

推荐答案

问题在于您的FileWriter对象是否正确.这意味着它将添加到文件中.如果您想将JSON对象正确地添加到文件中,建议您先阅读文件中的所有JSON对象.然后将它们存储在列表中,并将程序中的所有新条目添加到列表中.程序完成后,循环收集的JSON对象列表,并使用所有新旧JSON对象覆盖正在读取的文件.

The problem is with your FileWriter object and having the 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. 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.

程序启动时,请从"dbsettings.json"中读取并将所有这些JSON对象存储在某种数据结构中.如果稍后需要在程序中基于某个键找到JSON对象,则可以使用arraylist或Hashmap.

When program starts up, read from "dbsettings.json" and store all those JSON objects in some sort of data structure. An arraylist would work or perhaps a Hashmap if you need to find JSON objects later in your program based on some key.

然后在程序运行时,您将收到来自用户的有关新JSON对象的输入,只需将它们添加到您的数据收集中即可.每次获得新文件时,请勿将它们写入文件.我建议仅在用户干净退出程序时才使用集合中的所有JSON对象覆盖文件.这样,您可以确保每次启动程序时数据都采用正确的JSON格式.唯一的缺点是,如果程序意外退出,则会丢失在程序运行期间输入的所有数据.

Then as the program runs and you are receiving input from the user on new JSON objects just add them to your data collection. 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. That way you ensure that your data is in correct JSON form every time you start your program. 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();

这篇关于以正确的格式将数据写入JSON文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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