test_dg.py 1.12 KB
# -*- 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, '/')