How to write into a JSON file via input-box?

here is an example with an userInput :

String userInput = ""; 
int i; 

// list of planets 
JSONArray values = new JSONArray();

void setup() {

  size (700, 700);

  println("Attention, file gets overwritten"); 

  userInput = "1.6";

  addOneElement(userInput);  

  makeCompleteJsonObjectAndSave();
}

void draw() {
}

// -------------------------------------------------

void addOneElement(String userInput_) {

  // this can be executed when adding a new planet to values (collect user input before)

  JSONObject animal = new JSONObject();

  animal.setInt("id", i);
  animal.setString("species", userInput_);
  animal.setString("name", "Test 7");

  values.setJSONObject(i, animal);

  println (i
    +" :  "
    +values);

  i++;
}

void makeCompleteJsonObjectAndSave() {

  // this can be executed when leaving the program / you want to save all

  JSONObject json;

  json = new JSONObject();
  json.setJSONArray("animals", values);

  saveJSONObject(json, "data/new.json");
}
1 Like