Skip to main content

将 python 对象转换为 JSON 文档,反之亦然。

项目描述

JSON 转换

Codacy 徽章

Json Transform 允许您简单地将 Python 对象转换为 JSON 文档,反之亦然。

新的?这里有一些帮助:

例子

设置您的对象/实体。

from jsontransform import field, JSONObject


class Customer(JSONObject):
    def __init__(self):
        self._first_name = ""

    # set a custom name for the field because by default it will be the function name
    @property
    @field("firstName")
    def first_name(self):
        return self._first_name

    @first_name.setter
    def first_name(self, value):
        self._first_name = value

实例化对象并将其编码为 JSON 文档。

from jsontransform import dumpd, dump, dumps

new_customer = Customer()
new_customer.first_name = "Peter"

# get a dict representation of the object
dumpd(new_customer)
# result: {"firstName": "Peter"}

# get an str with with our encoded object
dumps(new_customer)
# result: '{"firstName": "Peter"}'

# we can also encode the object directly into a file
with open("new_customer.json", "w") as f:
    dump(new_customer, f)

JSON 文件 (new_customer.json):

{
  "firstName": "Peter"
}

解码 JSON 文档。

代码:

from jsontransform import load, loadd, loads

# we can decode our customer object from a JSON file
with open("new_customer.json", "r") as f:
    customer = load(f)

# or a dict
customer = loadd({"firstName": "Peter"})

# or an str as well
customer = loads("{'firstName': 'Peter'}")

customer.first_name
# result: Peter

更多的

项目详情


下载文件

下载适用于您平台的文件。如果您不确定要选择哪个,请了解有关安装包的更多信息。

源分布

json-transform-1.0.1.tar.gz (7.7 kB 查看哈希

已上传 source

内置分布

json_transform-1.0.1-py3-none-any.whl (7.3 kB 查看哈希

已上传 py3