How to get parse_json to parse multiple dictionaries?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By MustacheLincoln

I’m working on my first save system and it consists of multiple dictionaries. Simply the players character, the enemy, and a dictionary for the icon of each item they have equipped, so anywhere between 2 and 10 depending. However I can’t figure out how to parse past the first dictionary. They’re all saved on the same JSON file, which looks like this with three dictionaries:

{"AttackPower":10,"Boss":"res://Bosses/SkeletalArcher.tscn","Character":"res://Characters/PitFighter.tscn","DodgeChance":10,"Energy":100,"HP":110,"Potions":4,"SleepChance":5,"Speed":2,"_AttackPower":10,"_BossHP":80,"_DodgeChance":10,"_Energy":100,"_HP":89,"_Speed":2,"fv":151.5}
{"UpgradeIcon":"res://Upgrades/SandmansBlackjackIcon.tscn"}
{"AttackPower":7,"DodgeChance":20,"HP":100,"Speed":1.4,"_DodgeChance":20,"_HP":80}

and the code for parsing it looks like this:

save_game.open("res://saverun.save", File.READ)
while not save_game.eof_reached():
    var current_line = parse_json(save_game.get_as_text())

It works great for everything in the first dictionary, but the UpgradeIcon key and all the keys from the boss are completely ignored. I’m sure I’m just making a beginners mistake but I couldn’t seem to find a solution in the documentation.

:bust_in_silhouette: Reply From: HalfTough

You could just make them into one json:

{
"character" : {"AttackPower":10,"Boss":"res://Bosses/SkeletalArcher.tscn","Character":"res://Characters/PitFighter.tscn","DodgeChance":10,"Energy":100,"HP":110,"Potions":4,"SleepChance":5,"Speed":2,"_AttackPower":10,"_BossHP":80,"_DodgeChance":10,"_Energy":100,"_HP":89,"_Speed":2,"fv":151.5},
"enemy" : {"UpgradeIcon":"res://Upgrades/SandmansBlackjackIcon.tscn"},
"icon" : {"AttackPower":7,"DodgeChance":20,"HP":100,"Speed":1.4,"_DodgeChance":20,"_HP":80}
}

or

{"AttackPower":10,"Boss":"res://Bosses/SkeletalArcher.tscn","Character":"res://Characters/PitFighter.tscn","DodgeChance":10,"Energy":100,"HP":110,"Potions":4,"SleepChance":5,"Speed":2,"_AttackPower":10,"_BossHP":80,"_DodgeChance":10,"_Energy":100,"_HP":89,"_Speed":2,"fv":151.5},
{"UpgradeIcon":"res://Upgrades/SandmansBlackjackIcon.tscn"},
{"AttackPower":7,"DodgeChance":20,"HP":100,"Speed":1.4,"_DodgeChance":20,"_HP":80}]

How would you write the save function to make them save like that?

MustacheLincoln | 2018-05-17 09:28

You need to declare a single array which will contain all your dictionaries, then save that array into a JSON file.

Calinou | 2018-05-17 18:08

What would be the most elegant way to write that?

MustacheLincoln | 2018-05-20 17:41