test_dg.py
1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# -*- coding: utf-8 -*-
# 测试递归方法函数的使用--跟平台功能无关,但也不要删掉,谢谢!
import json
def restorkey(key):
with open("F:\\result.txt", "a") as f:
f.write(key)
def print_keyvalue_all(input_json, previous_key_str):
if isinstance(input_json, dict):
for key in input_json.keys():
key_str = previous_key_str + '/' + key
key_value = input_json.get(key)
if isinstance(key_value, dict):
print_keyvalue_all(key_value, key_str)
elif isinstance(key_value, list):
for json_array in key_value:
print_keyvalue_all(json_array, key_str)
else:
result = str(key_str) + " == " + str(key_value)
print result
restorkey("%s\n" % result)
elif isinstance(input_json, list):
for input_json_array in input_json:
print_keyvalue_all(input_json_array, previous_key_str)
if __name__ == '__main__':
with open('F:\\new.json') as json_file:
data = json.load(json_file)
print_keyvalue_all(data, '/')