从 json 文件导入后,将字典数据转换回其正确的数据类型

Convert data of a dictonary back to its proper data type after importing from a json file

我的问题类似于this

我正在使用 godot 游戏引擎,它使用 gdscript。我要求版主至少在我得到正确答复之前让它保留下来。我的项目快结束了,这是我唯一无法完成的事情。所以,希望有人能在这里帮助我。

我上传了三个词典到google玩游戏存档,当我回来的时候它给了我json数据。现在,我想将整数值转换回 int,将布尔值转换回 bool。

我的字典是这样的:

包含级别信息的词典:

GameDataManager.level_info =    'sand': {
        1:{ 
            'unlocked' : true, 
            'high_score' : 0, 
            'stars' : 0,
            'goals' : {
                'score' : 500
            },
            'level_to_load' : 'res://scenes/levels/sand/1.tscn'
            },
        2:{ 
            'unlocked' : false, 
            'high_score' : 0, 
            'stars' : 0,
            'goals' : {
                'score' : 2400
            },
            'level_to_load' : 'res://scenes/levels/sand/2.tscn'
            }
          },
'ice': {
            1:{ 
                'unlocked' : true, 
                'high_score' : 0, 
                'stars' : 0,
                'goals' : {
                    'score' : 700
                },
                'level_to_load' : 'res://scenes/levels/sand/1.tscn'
                },
            2:{ 
                'unlocked' : false, 
                'high_score' : 0, 
                'stars' : 0,
                'goals' : {
                    'score' : 2400
                },
                'level_to_load' : 'res://scenes/levels/sand/2.tscn'
                }
              }

还有更多,但这是示例。

Booster 字典如下所示:

BoosterManager.booster_info =       'powers' : {
            'plus_bomb' : {
                'total' : 0,
            },
            'hammer' : {
                'total' : 0,
            },
            'swap_pieces' : {
                'total' : 0,
            },
        },
        'booster' : {
            'color_bomb' : {
                'total' : 0,
                'set' : false,
            },
            'adjacent_bomb' : {
                'total' : 0,
                'set' : false,
            },
            'column_or_row_bomb' : {
                'total' : 0,
                'set' : false,
            },
        }
    }

现在,当我加载回这些数据时,我这样做是为了将数据类型转换回它们的原始状态:

func _on_game_load_success(data):
    var game_data: Dictionary = parse_json(data)
    GameDataManager.level_info.clear()
    BoosterManager.booster_info.clear()
    ChipsManager.chips_info.clear()
    GameDataManager.level_info = game_data['level_info']
    BoosterManager.booster_info = game_data['booster_info']
    ChipsManager.chips_info = game_data['chips_info']
    for i in GameDataManager.level_info:
        var temp_i = GameDataManager.level_info[i]
        str(i)
        GameDataManager.level_info[i] = temp_i
        for j in GameDataManager.level_info[i]:
            var temp = GameDataManager.level_info[i][j]
            int(j)
            GameDataManager.level_info[i][j] = temp
            GameDataManager.level_info[i][j]['high_score'] = int(GameDataManager.level_info[i][j]['high_score'])
            GameDataManager.level_info[i][j]['stars'] = int(GameDataManager.level_info[i][j]['stars'])
            GameDataManager.level_info[i][j]['unlocked'] = bool(GameDataManager.level_info[i][j]['unlocked'])
            for k in GameDataManager.level_info[i][j]['goals']:
                GameDataManager.level_info[i][j]['goals'][k] = int(GameDataManager.level_info[i][j]['goals'][k])
    for i in BoosterManager.booster_info['booster']:
        BoosterManager.booster_info['booster'][i]['total'] = int(BoosterManager.booster_info['booster'][i]['total'])
        BoosterManager.booster_info['booster'][i]['set'] = bool(BoosterManager.booster_info['booster']['set'])
    for i in BoosterManager.booster_info['power']:
        BoosterManager.booster_info['power'][i]['total'] = int(BoosterManager.booster_info['booster'][i]['power'])
    GameDataManager.save_data()
    BoosterManager.save_data()

但是当我尝试像这样引用它时 GameDataManager.level_info[lvl_type][lvl_num] 其中 lvl_type 是字符串而 lvl_num 是 int 我得到一个空错误。有人可以帮忙吗?

我解决了这个问题,但忘了post这里。这是我想出的代码并且有效。

func _on_game_load_success(data):
    var game_data: Dictionary = parse_json(data)
    var new_dict = {
        'sand' : {},
        'ice' : {},
        'spooky' : {},
        'city' : {}
    }
    for i in game_data.level_info:
        if i in new_dict.keys():
            for j in game_data.level_info[i]:
                new_dict[i][int(j)] = game_data.level_info[i][j]
        else:
            new_dict[i] = GameDataManager.level_info[i]
    for i in new_dict:
        for j in new_dict[i]:
            new_dict[i][j]['stars'] = int(new_dict[i][j]['stars'])
            new_dict[i][j]['high_score'] = int(new_dict[i][j]['high_score'])
            new_dict[i][j]['unlocked'] = bool(new_dict[i][j]['unlocked'])
            for k in new_dict[i][j]['goals']:
                new_dict[i][j]['goals'][k] = int(new_dict[i][j]['goals'][k])
    GameDataManager.level_info.clear()
    GameDataManager.level_info = new_dict
    BoosterManager.booster_info.clear()
    BoosterManager.booster_info = game_data.booster_info
    for i in BoosterManager.booster_info['booster']:
        BoosterManager.booster_info['booster'][i]['set'] = bool(BoosterManager.booster_info['booster'][i]['set'])
        BoosterManager.booster_info['booster'][i]['total'] = int(BoosterManager.booster_info['booster'][i]['total'])
    for i in BoosterManager.booster_info['powers']:
        BoosterManager.booster_info['powers'][i]['total'] = int(BoosterManager.booster_info['powers'][i]['total'])
    ChipsManager.chips_info.clear()
    ChipsManager.chips_info = game_data.chips_info
    ChipsManager.chips_info['chips'] = int(ChipsManager.chips_info['chips'])
    GameDataManager.save_data()
    BoosterManager.save_data()
    ChipsManager.save_data()