I'm currently using the Gson library to write/read a .json file. I have this method to write intro the json.
public static void write(String key, String value){
GeneralJsonConfig gjc = new GeneralJsonConfig();
if(key.equals("testKey")){
gjc.setaucString(value);
}
Gson gson = new Gson();
String json = gson.toJson(gjc);
try{
FileWriter fw = new FileWriter(launcherConfigFile);
fw.write(json);
fw.close();
} catch(IOException e) {
}
}
But lets say i have this .json:
{"testKey": "some test", "testKey2": "test 3"}
and i only want to change the thestKey from "some test" to another text and the other key/values will remain as they are now, but with my method the other values/key just dissaper, how can i solve this to make the other key/values stay ?
Update: Found an answer based on sam100rav answer, i simply read the complete json file to get the vaules and then write them again with the changed that i want done:
public static void write(String key, String value){
Gson gson = new GsonBuilder().setPrettyPrinting().create();
try{
BufferedReader br = new BufferedReader(new FileReader(launcherConfigFile));
GeneralJsonConfig gjcObject = gson.fromJson(br, GeneralJsonConfig.class);
if(key.equals("testKey")){
gjc.setaucString(value);
}
String json = gson.toJson(gjcObject);
FileWriter fw = new FileWriter(launcherConfigFile);
fw.write(json);
fw.close();
br.close();
} catch (IOException e){
main.er.LogError("23", "");
}