I would like to print this as a string:
", " So together with the double brackets. I tried:
" ",\n " "
""" ", " """
"\",\n \""
All of these do not work...
I would like to print this as a string:
", " So together with the double brackets. I tried:
" ",\n " "
""" ", " """
"\",\n \""
All of these do not work...
I am confused. You are trying to print ,
or ", "
?
For the first, the following should work:
print (", ")
If you are trying to print the string exactly, double quotes and all, then the following should work:
print ("\", \"")
The key is to print "
using \"
so Godot knows you want to print the "
character, not finish the string.
Both of the above code snippets work for me on Godot 3.1 on Windows 10, or at least they print correctly to the console. Hopefully this helps!
Thank you TwistedTwigleg. I have noticed this problem was caused by this code:
x.store\_string(JSON.print(data, " ", true))
It converts the data into json. But doing so, it does not convert things like \n into a space or a \" into a "
Any idea's how to get by this?
So now I get: { "answer": "answer1\",\"answer2\",\"answer3", "question": "question1\",\"question2\",\"question3" }
Instead of this: { "answer": "answer1","answer2","answer3", "question": "question1","question2","question3" }
And for those who neet to get the result ", " in a regular string. You can just create separate variables. Like:
var quotation = "\"" var space = "\n" var comma = "," And then just type
print(quotation + comma + space + quotation)
I don't know right off. The JSON output looks a little strange. I would expect the JSON output to look like one of the following:
# JSON dictionary output
{
"answer":
{
"answer1":"text here",
"answer2":"text here",
"answer3":"text here",
}
}
# JSON array output
{
"answer":
[
"answer1":"text here",
"answer2":"text here",
"answer3":"text here",
]
}
But I digress, so long as you can read/write the JSON data consistently and Godot understands it, I guess it doesn't matter how it is formatted.
While I do not know why you are getting /
in your results, an easy way to fix it is to replace any /
characters with a blank string, effectively removing the /
entirely. Something like this should, hopefully, work:
JSON.print(data, " ", true).replace("/", "")
However, it would probably be a better idea to find out why /
is being added and fix it there, instead of just removing it in post, though finding the fix is really dependent on your code and what you are doing.
Regardless, hopefully this helps!
Thank you. That in fact might help.
In the meantime I got some help with the code and it is working in a different way. Here it is. It writes a json file with a dictionary (but looking at wat you have written above, I guess it is an array) and you can append new strings to the existing keys and write them to the user folder.
var vraag_invoer
var bestandsnaam = "user://" + naam_bestand + ".json"
var antwoord_invoer
var data
func schrijven():#call this function from another function
#get the text input from the LineEdits
vraag_invoer = clone.get_node("VBoxContainer/HBoxContainer/LineEdit_vraag").text
antwoord_invoer = clone.get_node("VBoxContainer/HBoxContainer2/LineEdit_antwoord").text
var file = File.new()
#check of a file with that path exists
if not file.file_exists(bestandsnaam):
print("File does not exist")
return
#check if you can open the file
if file.open(bestandsnaam, File.READ) != 0:
print("Error opening file")
return
#get the content of the file as a text file
var raw = file.get_as_text()
#convert it to JSON
var parsed_json = JSON.parse(raw)
#data is what we are working with
data = parsed_json.result
#if the dictionary is empty, add a new key and string combination
if data.size() == 0: #if the dictionary is empty, add a new key and string combination
data["question"] = [vraag_invoer]
data["answer"] = [antwoord_invoer]
#write the info to the file
var data_string = JSON.print(data)
file = File.new()
file.open(bestandsnaam, File.WRITE)
file.store_string(data_string)
file.close()
#if not empty, just append the new strings
elif data.size() >0: #if the dictionary is not empty, append the new strings to the existing keys
data["question"].append(vraag_invoer)
data["answer"].append(antwoord_invoer)
#write the info to the file
var data_string = JSON.print(data)
file = File.new()
file.open(bestandsnaam, File.WRITE)
file.store_string(data_string)
file.close()