正则表达式修复格式错误的JSON

2024-09-30 06:14:40 发布

您现在位置:Python中文网/ 问答频道 /正文

所以基本上我得到了格式错误的JSON:

'status': {'$content': 'Completed'} 

'status': 'Completed'<;-期望

'foobar': {'$content': 1}

'foobar': 1<;-期望

我试图(([^{'\$content':])){'$content':不匹配,因为这总是没有用的

但是,我不知道如何让第二个组实际匹配正确的值,并去掉最后一个}

"SkillTreeModel":{"idToLevelDict2.0":{"TapDmg":2,"TapDmgFromHelpers":1,"PetDmg":0,"PetGoldQTE":13,"HeavyStrikes":16,"FireTapSkillBoost":0,"PetQTE":0,"Frenzy":0,"BossDmgQTE":0,"AllHelperDmg":15,"ChestGold":1,"HelperDmgSkillBoost":16,"ClanShipDmg":15,"HelperBoost":14,"HelperInspiredWeaken":1,"ClanQTE":16,"HelperDmgQTE":12,"ClanShipStun":0,"MPCapacityBoost":0,"MidasSkillBoost":0,"BurstSkillBoost":0,"CloneDmg":0,"Fairy":0,"ManaStealSkillBoost":0,"CloneSkillBoost":0,"ManaMonster":0,"CritSkillBoost":0,"BossTimer":0,"OfflineGold":11,"MultiMonsters":1,"CritSkillBoostDmg":3,"UltraDagger":1,"StrokeOfLuck":6,"SoulBlade":0,"Cloaking":1,"ForbiddenContract":0,"PoisonedBlade":0,"GuidedBlade":13,"None":0},"firstTimeSeeNewSkillTree":{"$content":true},"hasResetTreeThisPrestige":{"$content":false}}

如果有一种Python式的方式,我也会心存感激


Tags: ltjson格式status错误contentfoobarcompleted
2条回答

您可以使用object_hook^{}检查包含$content键的字典,并将其替换为值。请注意,您的JSON缺少包含{}的内容,因此您需要添加它们:

import json

def is_content(dct):
    if '$content' in dct:
        return dct['$content']
    return dct

d = json.loads('{"SkillTreeModel":{"idToLevelDict2.0":{"TapDmg":2,"TapDmgFromHelpers":1,"PetDmg":0,"PetGoldQTE":13,"HeavyStrikes":16,"FireTapSkillBoost":0,"PetQTE":0,"Frenzy":0,"BossDmgQTE":0,"AllHelperDmg":15,"ChestGold":1,"HelperDmgSkillBoost":16,"ClanShipDmg":15,"HelperBoost":14,"HelperInspiredWeaken":1,"ClanQTE":16,"HelperDmgQTE":12,"ClanShipStun":0,"MPCapacityBoost":0,"MidasSkillBoost":0,"BurstSkillBoost":0,"CloneDmg":0,"Fairy":0,"ManaStealSkillBoost":0,"CloneSkillBoost":0,"ManaMonster":0,"CritSkillBoost":0,"BossTimer":0,"OfflineGold":11,"MultiMonsters":1,"CritSkillBoostDmg":3,"UltraDagger":1,"StrokeOfLuck":6,"SoulBlade":0,"Cloaking":1,"ForbiddenContract":0,"PoisonedBlade":0,"GuidedBlade":13,"None":0},"firstTimeSeeNewSkillTree":{"$content":true},"hasResetTreeThisPrestige":{"$content":false}}}',
               object_hook = is_content)

print(d)

输出:

{'SkillTreeModel': {'firstTimeSeeNewSkillTree': True,
                    'hasResetTreeThisPrestige': False,
                    'idToLevelDict2.0': {'AllHelperDmg': 15,
                                         'BossDmgQTE': 0,
                                         'BossTimer': 0,
                                         'BurstSkillBoost': 0,
                                         'ChestGold': 1,
                                         'ClanQTE': 16,
                                         'ClanShipDmg': 15,
                                         'ClanShipStun': 0,
                                         'Cloaking': 1,
                                         'CloneDmg': 0,
                                         'CloneSkillBoost': 0,
                                         'CritSkillBoost': 0,
                                         'CritSkillBoostDmg': 3,
                                         'Fairy': 0,
                                         'FireTapSkillBoost': 0,
                                         'ForbiddenContract': 0,
                                         'Frenzy': 0,
                                         'GuidedBlade': 13,
                                         'HeavyStrikes': 16,
                                         'HelperBoost': 14,
                                         'HelperDmgQTE': 12,
                                         'HelperDmgSkillBoost': 16,
                                         'HelperInspiredWeaken': 1,
                                         'MPCapacityBoost': 0,
                                         'ManaMonster': 0,
                                         'ManaStealSkillBoost': 0,
                                         'MidasSkillBoost': 0,
                                         'MultiMonsters': 1,
                                         'None': 0,
                                         'OfflineGold': 11,
                                         'PetDmg': 0,
                                         'PetGoldQTE': 13,
                                         'PetQTE': 0,
                                         'PoisonedBlade': 0,
                                         'SoulBlade': 0,
                                         'StrokeOfLuck': 6,
                                         'TapDmg': 2,
                                         'TapDmgFromHelpers': 1,
                                         'UltraDagger': 1}}}

它仍然是一个有效的json,所以您可以转换为字典并根据需要更新键值

例:

my_dict=  {
  'SkillTreeModel': {
      # 'idToLevelDict2.0': {...},  # skipping other values
      'firstTimeSeeNewSkillTree': {
        '$content': True
      },
      'hasResetTreeThisPrestige': {
          '$content': False
      }
  }
}

假设您想从SkillTreeModel.hasResetTreeThisPrestige.content: False更新 到SkillTreeModel.hasResetTreeThisPrestige: False

import copy

new_dict = copy.deepcopy(my_dict)
for key, value in my_dict['SkillTreeModel'].items():
    if key == 'hasResetTreeThisPrestige':
        new_dict['SkillTreeModel']['hasResetTreeThisPrestige'] = value['$content']

print(new_dict)

相关问题 更多 >

    热门问题