Search code examples
javajsonfilejson-simple

Writing multiple times on a JSON file with Java


I'm trying to write multiple times on a JSON file using JSON-Simple and Java, but I have some problems after the second run. I'm new to JSON so that's just a way to learn about it, here is the code:

public class Writer{
    @SuppressWarnings("unchecked")
    public static void main(String[] args) throws IOException {
        JSONParser parser = new JSONParser();
        JSONObject outer = new JSONObject(); 
        JSONObject inner = new JSONObject();
        JSONObject data = new JSONObject();
        ArrayList<JSONObject> arr = new ArrayList<JSONObject>(); 
        inner.put("Name", "Andrea");
        inner.put("Email", "andrea@mail.com");
        arr.add(inner);
        outer.put("Clienti", arr);
        System.out.println("Dati: " + outer);   
        File file = new File("temp.json");
        if(file.exists()) {
                PrintWriter write = new PrintWriter(new FileWriter(file));
                Iterator<JSONObject> iterator = arr.iterator();
                while(iterator.hasNext()) {
                        JSONObject it = iterator.next();
                        data = (JSONObject) it; 
                    }
                arr.add(data);
                outer.put("Clienti", arr);
                System.out.println("Dati: " + outer);
                write.write(outer.toString());
                write.flush();
                write.close();
            } else {
                PrintWriter write = new PrintWriter(new FileWriter(file)); 
                write.write(outer.toString());
                write.flush();
                write.close();
            }
        }
    }

So, I just wanna try to add the same thing without losing what I added before, but when I run:

  • The first run goes well, it prints normally on the file. Result:

Dati: {"Clienti":[{"Email":"andrea@gmail.com","Nome":"Andrea"}]}

  • The second run too, it adds another field inside the list, keeping the first one too. Result:

Dati: {"Clienti":[{"Email":"andrea@gmail.com","Nome":"Andrea"},{"Email":"andrea@gmail.com","Nome":"Andrea"}]}

  • From the third run it doesn't upload anymore the file, instead of adding another field to the existent 2 it just prints the second result.

I tried many options but still can't understand how to add a third field without losing the previous two, how can i solve this?


Solution

  • Solved putting this on if clause:

    if(file.exists()) {
                Object obj = parser.parse(new FileReader("temp.json"));
                JSONObject jsonObject = (JSONObject) obj;
                JSONArray array = (JSONArray) jsonObject.get("Clienti");
                PrintWriter write = new PrintWriter(new FileWriter(file));
                Iterator<JSONObject> iterator = array.iterator();
                while(iterator.hasNext()) {
                    JSONObject it = iterator.next();
                    data = (JSONObject) it; 
                    System.out.println("Data" + data);
                    arr.add(data);
                    }
                arr.add(inner);
                System.out.println(arr);
                outer.put("Clienti", arr);
                System.out.println("Dati: " + outer);
                write.write(outer.toString());
                write.flush();
                write.close();
        }