{
"duncan_long": {
"id": "drekaner",
"name": "Duncan Long",
"favorite_color": "Blue"
},
"kelsea_head": {
"id": "wagshark",
"name": "Kelsea Head",
"favorite_color": "Ping"
},
"phoenix_knox": {
"id": "jikininer",
"name": "Phoenix Knox",
"favorite_color": "Green"
},
"adina_norton": {
"id": "slimewagner",
"name": "Adina Norton",
"favorite_color": "Red"
}
}我正在尝试返回除用户id之外的所有用户的JSON列表
发布于 2020-11-28 03:24:20
假设包含JSON的文件名为file.json
import json
with open('file.json') as f:
d = json.loads(f)
for key, value in d.items():
del value['id']
d[key] = value替代方法您可以使用以下方法:
import json
with open('file.json') as f:
d = json.loads(f)
for key, value in d.items():
value.pop('id', None) // this will not crash if the element has no key 'id'发布于 2020-11-28 03:24:44
import json
with open('file.json') as fin:
your_structure = json.load(fin)
for value in your_structure.values():
value.pop('id', None)https://stackoverflow.com/questions/65042790
复制相似问题