Hi there. I’d like to know the following: Is it possible to save user-input straightly into a specific array position in a JSON file?
Example:
The user get’s a little screen in Processing. It states the question: “How much gravity has the planet you’re on?” and the user types 1.6 and then that specific answer, whether it’s a number or a text, gets saved in the last slot for gravity. After that the user would be able to click to the next question and it would just go down the list. Please let me know if this is possible and how I could achieve this. Thank you!
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
That is correct. I’d also like to add that the link you sent is exactly what I’m looking for. However, I want to know if a user could write the info straight into the array, that’s then saved into a JSON file.
In my previous post (and code) I had this screen pop up to the user, where they could answer the questions and click the button to move on to the next one.
Hopefully I’m clear enough. So, to state the question again: Is it possible to write into a box, like the image below shows, and have that answer be saved into the array of a JSON file?
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");
}
As to answer your question about why it has to be JSON, it’s because I want to gather user data so I could import it into After Effects (which supports JSON files), where I can then link the data to Visual FX and animation parameters.
This may not be fully logical, since you could also enter that data manually in After Effects, but the whole driver behind this little project is the fact that it’s a school project that I have to finish in 1.5 weeks - and I’m kinda running low on time
esp. if you make a data file used in another system ( computer or program )
your database structure should be good.
++ your start with the first JSON object including a array using good descriptors
? sorry i not know the correct name for a field like “universe” ?
( JSON has its own wording i think )
already.
still any program using this ( incl. the processing program we talk about here )
would need to have the question text “Give the size of the universe” hardcoded
and the other system, reading the answers, would not know the questions!!
also what i mentioned already,
to have the user answers in one field and the correct answers
like for
atmosphere ?==? oxygen
gravity ?==? 2.4
in a second field makes sense.
here an example to check and score on that: ( score is a database field too ! )
int newscore = 0;
if ( question.getString("q1user").equals( question.getString("q1check") ) ) newscore++;
if ( question.getFloat("q2user") == question.getFloat("q2check") ) newscore++;
question.setInt("score",newscore);