0

how to save data from our textfields. For example i want to get this:

[
  {
      "Patient": {
      "name": "John",
      "surname": "Cena"

      }
    },
  {
  "Patient2": {
    "name": "Roger",
    "surname": "Federer"
  }
  }
]

And it was my try:

JSONObject obj = new JSONObject();
obj.put("imie", field1.getText());
obj.put("nazwisko", field2.getText());

try (FileWriter Data = new FileWriter("Data.JSON")) {
    Data.write(obj.toJSONString());
    Data.write(obj1.toJSONString());
    } catch (IOException e1) {
        e1.printStackTrace();
    }

but i dont get "Patient2" and it overwriting my first patient if i press save button instead of add new one.

1 Answers1

0

You should be using JSONArray to store several JSONObject instances:

// build object
JSONObject obj = new JSONObject();
obj.put("name",    field1.getText());
obj.put("surname", field2.getText());

// build "patient"
JSONObject patient = new JSONObject();
patient.put("patient", obj);

// build another object
JSONObject obj1 = new JSONObject();
obj1.put("name",    "Roger");
obj1.put("surname", "Federer");

// build another patient
JSONObject patient1 = new JSONObject();
patient1.put("patient1", obj1);

// create array and add both patients
JSONArray arr = new JSONArray();

arr.put(patient);
arr.put(patient1); 

try (FileWriter Data = new FileWriter("Data.JSON")) {
    Data.write(arr.toString(4)); // setting spaces for indent
} catch (IOException e1) {
     e1.printStackTrace();
}

This code produces JSON:

[
    {
        "patient": {
            "surname": "Doe",
            "name": "John"
        }
    },
    {
        "patient1": {
            "surname": "Federer",
            "name": "Roger"
        }
    }
]
Nowhere Man
  • 19,170
  • 9
  • 17
  • 42
  • Oh, i already figure it out, but my problem is about saving next patient from textfields if i press button second time, not from code. I think that i have to use for loop or something but dont know how :c – Maciek Groszyk Apr 27 '20 at 14:27
  • Sorry, I don't understand what you mean by saving _not from code_. It seems you need to have at least two buttons `[Add Patient]`, and `[Save]`. When you click `[Add Patient]` you read text fields, create new patient with the text field data and add her/him to array. When you click `[Save]`, you write the array to the file. – Nowhere Man Apr 27 '20 at 14:48
  • @MaciekGroszyk, when you are working with `JSON Array` you need to deserialise whole array into `Java` object, `JSONArray`, add new element and serialise and overwrite it back to file. Take a look on similar questions with another `JSON` libraries: [How can I use gson to create a set of key value pairs?](https://stackoverflow.com/questions/58975277/how-can-i-use-gson-to-create-a-set-of-key-value-pairs), [How to append to JSON with Java and Jackson](https://stackoverflow.com/questions/58888004/how-to-append-to-json-with-java-and-jackson) – Michał Ziober Apr 27 '20 at 15:08
  • Ok i will check this topics and just come back here if its doesnt help – Maciek Groszyk Apr 27 '20 at 15:48