Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用marshmellow Python将Json转换为对象列表_Python_Json_Flask_Serialization_Marshmallow - Fatal编程技术网

使用marshmellow Python将Json转换为对象列表

使用marshmellow Python将Json转换为对象列表,python,json,flask,serialization,marshmallow,Python,Json,Flask,Serialization,Marshmallow,我有json和我的对象的数据 我创建了将其序列化到对象列表中的模式,但它不起作用 模式: from marshmellow import fields, Schema class ContactSchema(Schema): first_name = fields.String(attribute="ftNm") last_name = fields.String(attribute="ltNm") phone = fields.

我有json和我的对象的数据 我创建了将其序列化到对象列表中的模式,但它不起作用

模式:

from marshmellow import fields, Schema

class ContactSchema(Schema):
    first_name = fields.String(attribute="ftNm")
    last_name = fields.String(attribute="ltNm")
    phone = fields.Integer(attribute="pn")
型号:

class Contact:
    id: int
    first_name: str
    last_name: str
    phone: str
我有一个转换函数(但不工作)


如果有人能帮我将json转换成对象列表,我将不胜感激。我不太清楚您的意图。但是,序列化和反序列化可以通过以下方式实现

通过重命名变量名指定的属性进行序列化和反序列化

from marshmallow import Schema, fields
    
class ContactSchema(Schema):
    first_name = fields.Str(attribute="ftNm", data_key="ftNm")
    last_name = fields.Str(attribute="ltNm", data_key="ltNm")
    phone = fields.Integer(attribute="pn", data_key="pn")

# serialization to json
def from_list():
    data = [
        {'ftNm': 'Name1', 'ltNm': 'Surname1', 'pn': 343434},
        {'ftNm': 'Name2', 'ltNm': 'Surname2', 'pn': 141414},
        {'ftNm': 'Name3', 'ltNm': 'Surname3', 'pn': 656565}
    ]
    schema = ContactSchema(many=True)
    return schema.dump(data)

# deserialization from json
def to_list():
    json = [
        {'ftNm': 'Name1', 'ltNm': 'Surname1', 'pn': 343434},
        {'ftNm': 'Name2', 'ltNm': 'Surname2', 'pn': 141414},
        {'ftNm': 'Name3', 'ltNm': 'Surname3', 'pn': 656565}
    ]
    schema = ContactSchema(many=True)
    return schema.load(json)
class ContactSchema(Schema):
    first_name = fields.Str(attribute="ftNm")
    last_name = fields.Str(attribute="ltNm")
    phone = fields.Integer(attribute="pn")

# deserialization from json
def to_list():
    json = [
        {'first_name': 'Name1', 'last_name': 'Surname1', 'phone': 343434},
        {'first_name': 'Name2', 'last_name': 'Surname2', 'phone': 141414},
        {'first_name': 'Name3', 'last_name': 'Surname3', 'phone': 656565}
    ]
    schema = ContactSchema(many=True)
    return schema.load(json)
反序列化而不重命名由变量名指定的属性

from marshmallow import Schema, fields
    
class ContactSchema(Schema):
    first_name = fields.Str(attribute="ftNm", data_key="ftNm")
    last_name = fields.Str(attribute="ltNm", data_key="ltNm")
    phone = fields.Integer(attribute="pn", data_key="pn")

# serialization to json
def from_list():
    data = [
        {'ftNm': 'Name1', 'ltNm': 'Surname1', 'pn': 343434},
        {'ftNm': 'Name2', 'ltNm': 'Surname2', 'pn': 141414},
        {'ftNm': 'Name3', 'ltNm': 'Surname3', 'pn': 656565}
    ]
    schema = ContactSchema(many=True)
    return schema.dump(data)

# deserialization from json
def to_list():
    json = [
        {'ftNm': 'Name1', 'ltNm': 'Surname1', 'pn': 343434},
        {'ftNm': 'Name2', 'ltNm': 'Surname2', 'pn': 141414},
        {'ftNm': 'Name3', 'ltNm': 'Surname3', 'pn': 656565}
    ]
    schema = ContactSchema(many=True)
    return schema.load(json)
class ContactSchema(Schema):
    first_name = fields.Str(attribute="ftNm")
    last_name = fields.Str(attribute="ltNm")
    phone = fields.Integer(attribute="pn")

# deserialization from json
def to_list():
    json = [
        {'first_name': 'Name1', 'last_name': 'Surname1', 'phone': 343434},
        {'first_name': 'Name2', 'last_name': 'Surname2', 'phone': 141414},
        {'first_name': 'Name3', 'last_name': 'Surname3', 'phone': 656565}
    ]
    schema = ContactSchema(many=True)
    return schema.load(json)
可能无法正确指示转换方向

from marshmallow import Schema, fields, post_load
from dataclasses import dataclass

@dataclass
class Contact:
    # id: int
    first_name: str
    last_name: str
    phone: str

class ContactSchema(Schema):
    first_name = fields.Str(data_key="ftNm")
    last_name = fields.Str(data_key="ltNm")
    phone = fields.Integer(data_key="pn")

    @post_load
    def make_user(self, data, **kwargs):
        return Contact(**data)

# deserialization from json
def to_list():
    json = [
        {'ftNm': 'Name1', 'ltNm': 'Surname1', 'pn': 343434},
        {'ftNm': 'Name2', 'ltNm': 'Surname2', 'pn': 141414},
        {'ftNm': 'Name3', 'ltNm': 'Surname3', 'pn': 656565}
    ]
    schema = ContactSchema(many=True)
    return schema.load(json)

要序列化和反序列化数据库模型,我建议使用and。

我不知道它是否正常,但我只是创建了一个简单的函数并通过循环进行反序列化

    def create(self, json: dict):
        key = Key()
        key.id = json.get("id")
        key.key_number = json.get("num")
        key.hash = json.get("hash")

        return key

但不起作用
–有什么错误?只有逻辑错误,我只需要正确的函数Marschmallow具有许多预定义字段的优势,并且可以验证它们。另请参见marshmalllow和webargs的组合。尤其是当你的项目越来越大时,它可能会帮助你。我是小而简单的解决方案的朋友,如果您的方法符合期望,我将为您感到高兴。