是否有一个python库可以将JSON模式转换为python类定义,类似于Java的jsonschema2pojo--https://github.com/joelittlejohn/jsonschema2pojo

推荐答案

到目前为止,我所能找到的最接近的是warlock,它宣传这个工作流:

Build your schema

>>> schema = {
    'name': 'Country',
    'properties': {
        'name': {'type': 'string'},
        'abbreviation': {'type': 'string'},
    },
    'additionalProperties': False,
}

Create a model

>>> import warlock
>>> Country = warlock.model_factory(schema)

Create an object using your model

>>> sweden = Country(name='Sweden', abbreviation='SE')

However, it's not quite that easy. The objects that Warlock produces lack much in the way of introspectible goodies. And if it supports nested dicts at initialization, I was unable to figure out how to make them work.

To give a little background, the problem that I was working on was how to take Chrome's JSONSchema API and produce a tree of request generators and response handlers. Warlock doesn't seem too far off the mark, the only downside is that meta-classes in Python can't really be turned into 'code'.

Other useful modules to look for:

If you end up finding a good one-stop solution for this please follow up your question - I'd love to find one. I poured through github, pypi, googlecode, sourceforge, etc.. And just couldn't find anything really sexy.

For lack of any pre-made solutions, I'll probably cobble together something with Warlock myself. So if I beat you to it, I'll update my answer. :p

Json相关问答推荐

使用Jolt变换转换JsonArray以将关键字转移到内部JsonArray中

Vega-Lite:文本笔画在外部

如何使用表键名称GROUP_BY

对面的行/列进行排序时可能出现错误

当console.log返回TRUE值时,解析的JSON中的字段未定义

nlohmann json:为什么我会得到一个解析错误?

用巨大的值更新SQL Server中的nvarchar(max)

在 python 中循环 JSON 数组

使用 JQ 获取 JSON 中的替代元素(输出:JSON 对象)

添加到数组时出错:找不到Add的重载和参数计数:1

解析 JSON API 响应

如何使用 jackson 反序列化为 Kotlin 集合

如何使用 C# 将 JSON 文本转换为对象

在 Webpack 中加载静态 JSON 文件

Java循环遍历Json数组?

在浏览器中查看 JSON 文件

将序列化表单的数据转换为 json 对象

有没有办法折叠 Postman 中的所有 json 字段

有 Json 标签但未导出

从 JSON 到 JSONL 的 Python 转换