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

ok, thanks
it does save a file but please

  • use the /data/filename.extension
    path and not the sketch path.
  • better use filename string variables
  • you save a string array with saveStrings, that is not JSON

but it does work, so i must ask

if you want to learn that you again start with my first example and
try to get the array and data ( out of the from file already loaded JSON object )
try:

JSONObject json_o;

String infile  = "data/universe.json";
String outfile = "data/universe_answers.json";

void setup() {
  println("load from "+infile);
  json_o = loadJSONObject(infile);
  get_content();
  println("use:\n [s] to save data to file");
}

void get_content() {
  JSONArray universe = json_o.getJSONArray("universe");
  for ( int i=0; i < universe.size(); i++ ) {
    JSONObject planet  = universe.getJSONObject(i);
    String type =  planet.getString("type");
    String atmo =  planet.getString("atmosphere");
    Float  grav =  planet.getFloat("gravity");
    println ("#i: "+i+" planet type: "+ type +" atmosphere: "+ atmo +" gravity: "+ grav);
  }
}

void draw() {
}

void keyPressed() {
  if ( key == 's' ) { 
    saveJSONObject(json_o, outfile);
    println("save to "+outfile);
  }
}

shows:

load from data/universe.json
#i: 0 planet type: A6 atmosphere: none gravity: 1.6
#i: 1 planet type: C2 atmosphere: oxygen gravity: 2.4
#i: 2 planet type: K5 atmosphere: sulfur gravity: 5.1
use:
 [s] to save data to file

and change the values according any user input.

so just to go more into the question / answer idea

? what you think of the idea to have 2 value fields /

  • the user answer
  • the correct value

so the structure of read and save file can be same. just the “loadfile” has a empty user answer field
and the answer check coding ( you need to do later gets more easy )

( about the input window and the click button can talk later… )
could look like
SNAG-0068