我想创建一个函数来递归遍历一个多维字典,其中维度是未知的。
以下是我迄今为止想到的内容,但似乎并没有正确工作。它将会打印出某些键/值两次,并且它们不是按顺序排列的。
def walk_dict(d):
for k,v in d.items():
if isinstance(v, dict):
walk_dict(v)
else:
print "%s %s" % (k, v)
以下是一个示例数组:
d = {
'plan_code': 'b',
'quantity': '1',
'account': {
'account_code': 'b',
'username': 'jdoe',
'email': 'jdoe@domain.com',
'first_name': 'b',
'last_name': 'b',
'company_name': 'Company, LLC.',
'billing_info': {
'first_name': 'b',
'last_name': 'b',
'address1': '123 Test St',
'city': 'San Francisco',
'state': 'CA',
'country': 'US',
'zip': '94105',
'credit_card': {
'number': '1',
'year': '2018',
'month': '12',
'verification_value': '123',
},
},
},
}
json.dumps
无法处理结构中的某些对象。 - OrangeDogjsonpickle
库在表示json
无法处理的对象类型方面表现出色 - 可以尝试使用它。 - Chris Johnson