简体   繁体   中英

Write data in JSON file in right form JAVA

I have question about the form of the data input in 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);

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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