删除json文件中所有出现的键

如何删除json文件中所有出现的键?在下面的示例中,我要删除所有“评级”键。

现在如何:

{
  "player": {
    "rating": "99",
    "rarity": "super_rare"
  },
  "player2": {
    "rating": "87",
    "rarity": "rare"
}

我想要的是:

{
  "player": {
    "rarity": "super_rare"
  },
  "player2": {
    "rarity": "rare"
}
评论
fomnis
fomnis

尝试这个:

data = json.loads('data.json')
for player in data:
    del data[player]['rating']
print(data)

输出:

{'player': {'rarity': 'super_rare'}, 'player2': {'rarity': 'rare'}}
点赞
评论