0 votes

Hello community,
I have a problem with JSON parsing

I have a dictionary that I save as a line to the file.json

func save_to_file():
   save_to_slot("slot_1")

   yield(get_tree().create_timer(0.5), "timeout")
   var save_game = File.new()

   save_game.open("user://slot_1.json", File.WRITE)
   save_game.store_line(JSON.print(data2save.duplicate()))

   save_game.close()

Then I load it

func load_from_file():
   var save_game = File.new()

   if not save_game.file_exists("user://slot_1.json"):
       print("File %s doesn't exist.","user://slot_1.json")
       return

   save_game.open("user://slot_1.json", save_game.READ)
   var text = save_game.get_as_text()

   var data = JSON.parse(text).result

   save_game.close()

   yield(get_tree().create_timer(0.5), "timeout")
   slots["slot_1"] = data

   yield(get_tree().create_timer(0.5), "timeout")

   load_from_slot("slot_1")

data structure looks like this:
data2save = { [[map_filename, node_name], node_data] }

real data example would be:
{ "[res://level1.tscn, player1]":{"HP":10,"weapon":"sword","bDead":False}}

when JSON parses the save_game.get_as_text() variable the Array key becomes String

Any idea how can I convert it back to an Array key? If you want more details ask me anytime

in Engine by (14 points)

I remember I had a similar issue. I ended up making a custom string2array() function and saved it in a global/autoload file so I could re-use it.

Basically it took a string as a parameter, then created a new array, parsed the string and stored the parse string into the new array. Then the function returned the new array.

Please log in or register to answer this question.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.