Trying to get a struct to a variable from a .txt or .json file

Hey all, I want to use a txt or json file containing a struct with all the dialogue for the game (so I could swap in translations etc) but with all my attempts at searching how to do this correctly I still haven't managed to.

If I have a .txt or .json file containing:
{
"teststring": "this is a test message"
}

How do I go about getting that content to a single variable as a struct? I have tried including the file to the project by adding it to the data folder, then trying different combinations of file_text_open_read, file_text_read_string and json_parse functions, but no luck.

Thanks in advance!
 
Read all contents of the file into a string, then parse it using json_parse.
I really feel like I have tried my best to do exactly this, but haven't gotten it to work.

Can you tell me what would be wrong with:

Mystruct = json_parse(file_text_read_string(file_text_open_read(dialogue.json)));

Also, could I then (if it would work) acces the string in the struct with Mystruct.teststring ?

Thank you :)
 

TsukaYuriko

☄️
Forum Staff
Moderator
I really feel like I have tried my best to do exactly this, but haven't gotten it to work.

Can you tell me what would be wrong with:

Mystruct = json_parse(file_text_read_string(file_text_open_read(dialogue.json)));
You got pretty close. In the opening post, it looks like your file contains multiple lines. You are only reading a single line here, though. You can use the example on file_text_eof's manual page for reference.

Also, could I then (if it would work) acces the string in the struct with Mystruct.teststring ?
Yes.
 
Top