0

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", "");
        }

2 Answers 2

1
JSONObject obj = new JSONObject();
obj.put("testKey", "some test");
obj.put("testKey2", "test 3");
if(key.equals("testKey")){
        obj.put("testKey", value);
    }
    String json = obj.toString();
    try{
    FileWriter fw = new FileWriter(launcherConfigFile);
    fw.write(json);
    fw.close();
    } 
Sign up to request clarification or add additional context in comments.

Comments

0

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", "");
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.