So I tried to write some code using what I found out on different sites but nothing works. Every time I try to write in to the file it doesn't work.
Here is the code for writing and reading from and in a JSON file:
HashMap<Word, ArrayList<Word>> map=null;
JSONParser parser = new JSONParser();
try {
Object obj = parser.parse(new FileReader("Dictionar.json"));
JSONObject jsonObject = (JSONObject) obj;
map=(HashMap) jsonObject;
} catch (Exception e) {
e.printStackTrace();
}
Dictionar dictionar=new Dictionar();
dictionar.setDictionar(map);
Here is the code for writing to the file:
JSONObject obj = new JSONObject();
obj.putAll(dictionar);
File f=new File("Dictionar.json");
f.createNewFile();
FileWriter file = new FileWriter(f);
obj.writeJSONString(file);
System.out.println("Successfully Copied JSON Object to File...");
System.out.println("\nJSON Object: " + obj);
}
catch (IOException i) {
i.printStackTrace();
}
The variable dictionar
is a HashMap.