How to open a JSON file in Godot that contains 2 arrays?

  • Thread starter Thread starter Darkmisc
  • Start date Start date
  • Tags Tags
    godot
AI Thread Summary
The discussion revolves around saving and loading two arrays in a JSON file using code. The user successfully saves the arrays into a JSON format, which appears correctly when viewed in Notepad. However, when attempting to load the arrays back, the code returns a null value for the parse result. The issue is identified as a mistake in the loading function, where the user incorrectly references "arrayA" instead of "arrayB" when assigning values to Global.arrayD. Ultimately, the user resolves the problem by deciding to save the arrays into a dictionary instead of using JSON, indicating a shift in approach for data storage.
Darkmisc
Messages
222
Reaction score
31
TL;DR Summary
I've saved two arrays in a JSON file, but can't seem to open them again.
Hi everyone

I've saved two arrays into a JSON file.

This was the code I used:
[CODE lang="python" title="save"]func save():
var file = FileAccess.open("user://savedarrays.json", FileAccess.WRITE)
var file_path = "user://saved_arrays.json"

var data = {
"arrayA": Global.arrayA,
"arrayB": Global.arrayB
}
var json_string: String = JSON.stringify(data)
file.store_string(json_string)
file.close()
print("Arrays saved successfully.")

[/CODE]

When I open the file in notepad, this is what I get:
{"arrayA":["France","Spain","UK","Russia"],"arrayB":["Paris","Madrid","London","Moscow"]}

I'm using the following code to load the arrays:

[CODE lang="python" title="load"]func loadArraysFromFile():

var file = "user://savedarrays.json"
var file_text = FileAccess.get_file_as_string(file)
var parse_result = JSON.parse_string(file_text)
print(parse_result)

if parse_result.error == OK:
var parsed_data: Dictionary = parse_result.result as Dictionary

if parsed_data.has("arrayA"):
Global.arrayC = parsed_data["arrayA"]
print(Global.arrayC)
if parsed_data.has("arrayB"):
Global.arrayD = parsed_data["arrayA"]
print(Global.arrayD)[/CODE]

It prints out <null> as the parse_result.

Does anyone know what I've done wrong with the load code?

I used JSON because I read somewhere that that's what I need if I want to save two arrays under the one filename.

Elsewhere, I've used the following code to save one variable to a file:
[CODE lang="python" title="save one var"] var file = FileAccess.open(save_path, FileAccess.WRITE)
file.store_var(Global.thing)[/CODE]Thanks
 
Technology news on Phys.org
Nvm. I saved the two arrays into a dictonary and saved the dictionary without using JSON.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Replies
13
Views
3K
Replies
23
Views
37K
Back
Top