Want to post this question here as well. I Have big problems trying to write a new key and string to an existing blank json dictionary.
And I want to know how afterwards to append a new string to the existing key.
var e = "{}"
var data
func bestand_schrijven(): #function for writing a JSON file with the name given by the user. Called naam_bestand
var file = File.new()
var verbergen = get_node("VBoxContainer_general/HBoxContainer_naam")
bestandsnaam = "user://" + naam_bestand + ".json" #moet user:// worden
if file.file_exists(bestandsnaam): #check if the file name already exists
bestaat = true
get_node("VBoxContainer_general/HBoxContainer_naam/Label_naam_bestand").text = "\n\nNaam bestaat al\nProbeer opnieuw" #message to user that the file exists
timer.set_wait_time( 3 )
timer.start()
else: #if the file does not exist, do following
bestaat = false
file.open(bestandsnaam, File.WRITE)
file.store_string(e) #put the empty dictionary e inside the file
file.close()
func dictionary_schrijven(): #this is to write the dictionary content
#this is to open up the file
var x = File.new()
x.open(bestandsnaam, File.READ)
var json = JSON.parse(x.get_as_text())
x.close()
var data = json.result
#function to edit the file
schrijven()
#safe the file
x = File.new()
x.open(bestandsnaam, File.WRITE)
x.store_string(JSON.print(data, " ", true))
x.close()
func schrijven():
data["question"] = vraag_invoer # I want to create the key question. vraag_invoer is the output of the LineEdit in the instance
data["answer"] = antwoord_invoer # same here for the key answer and the text output of the other LineEdit