斯蒂芬大帝
这是我很久以前用于类似目的的东西,希望你会发现它有帮助,或者至少是解决你的问题的良好起点!import json# Define default values to use for each type, adjust as neededdefault_values = { 'string': '', 'int': 0, 'integer': 0, 'number': 0, 'array': [], 'list': [], 'tuple': [], 'dict': {}, 'object': {}, 'boolean': 'false',}# Iterate the schema and return simplified dictionary or JSON stringdef schema_to_json(s, to_json=False): res = {k: default_values[v['type']] for k, v in s.items()} return json.dumps(res) if to_json else res# Pass your schema to function and get simplified version backschema = { 'name': {'type': 'string'}, 'age': {'type': 'integer'},}py_dict = schema_to_json(schema) # Will return Python dictionaryjson_string = schema_to_json(schema, True) # Will return JSON string