我正在尝试打印出一些json的地图,以便我能够快速了解它的结构。我有一个函数,递归循环遍历嵌套的json并打印项的所有键。但是,它没有像我期望的那样运作。 example json
day = datetime.datetime.strptime('{0}/{1}/{2}'.format(M, D, Y), '%m/%d/%Y').strftime('%A')
我希望输出看起来像这样:
def inspect_json(level,nested_json):
for key in nested_json.keys():
print "{}.{}".format(level,key)
if isinstance(nested_json[key],dict):
level += 1
inspect_json(level,nested_json[key])
>>> inspect_json(1,data)
1.@xmlns:aws
1.aws:OperationRequest
2.aws:RequestId
2.aws:UrlInfoResult
3.aws:Alexa
4.aws:ContentData
5.aws:DataUrl
6.@type
etc...
答案 0 :(得分:2)
我想你想要这样的东西:
def inspect_json(level,nested_json):
for key in nested_json.keys():
print "{}.{}".format((level-1)*' ',key) # identation 2 blancs
if isinstance(nested_json[key],dict):
inspect_json(level+1,nested_json[key]) # increment level