尝试从Json中展平别名键
{
"name": "Rocky Marci",
"aliases": ["Rocky", "Champ"],
"physical": {
"height_in": 67,
"weight_lb": 150
},
"Fights": 49
}
就像下面这样
{
"name": "Rocky Marci",
"aliases.0": "Rocky",
"aliases.1": "Champ",
"physical.height_in": 67,
"physical.weight_lb": 150,
"Fights": 49
}
我试过了
def flatten(d, parent_key='', sep='_'):
items = []
for k, v in d.items():
new_key = parent_key + sep + k if parent_key else k
if isinstance(v, collections.MutableMapping):
items.extend(flatten(v, new_key, sep=sep).items())
else:
items.append((new_key, v))
return dict(items)
但这并不能使阵列变平
因为list不是
collections.MutableMapping
的实例。我建议查查相反。你知道吗
更多的可能性是here
可以使用递归:
输出:
相关问题 更多 >
编程相关推荐