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

  • Thread starter Darkmisc
  • Start date
  • Tags
    godot
  • #1
Darkmisc
220
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:
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.")

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:

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)

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:
save one var:
    var file = FileAccess.open(save_path, FileAccess.WRITE)
    file.store_var(Global.thing)
Thanks
 
Technology news on Phys.org
  • #2
Nvm. I saved the two arrays into a dictonary and saved the dictionary without using JSON.
 

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

How do I load a JSON file in Godot?

To load a JSON file in Godot, you can use the `File` class to read the file and the `JSON.parse` method to convert the JSON string into a Dictionary. Here is a basic example:```var file = File.new()if file.file_exists("res://path_to_your_file.json"): file.open("res://path_to_your_file.json", File.READ) var json_string = file.get_as_text() file.close() var result = JSON.parse(json_string) if result.error == OK: var data = result.result print(data) else: print("Error parsing JSON")```

How can I access the arrays within the JSON file?

Once you have parsed the JSON file into a Dictionary, you can access the arrays by using the appropriate keys. For example, if your JSON structure is:```{ "array1": [1, 2, 3], "array2": [4, 5, 6]}```You can access the arrays like this:```var array1 = data["array1"]var array2 = data["array2"]print(array1) # Output: [1, 2, 3]print(array2) # Output: [4, 5, 6]```

What should I do if the JSON file contains nested arrays or objects?

If your JSON file contains nested arrays or objects, you can access them in a similar manner by chaining the keys. For example, if your JSON looks like this:```{ "nested": { "array1": [1, 2, 3], "array2": [4, 5, 6] }}```You can access the nested arrays like this:```var nested = data["nested"]var array1 = nested["array1"]var array2 = nested["array2"]print(array1) # Output: [1, 2, 3]print(array2) # Output: [4, 5, 6]```

How can I handle errors when opening or parsing the JSON file?

To handle errors, you should check the return values of the `File.open` and `JSON.parse` methods. If `File.open` fails, it will return an error code. Similarly, `JSON.parse` will return a `JSONParseResult` object that contains an error code. Here is how you can handle errors:```var file = File.new()if file.file_exists("res://path_to_your_file.json"): var error = file.open("res://path

Similar threads

Replies
4
Views
1K
Replies
13
Views
3K
Replies
23
Views
36K
Back
Top