【问题标题】:Json serialization pythonjson序列化python
【发布时间】:2015-10-08 14:59:11
【问题描述】:

我有 Employee 类,其中包含姓名 age 和 Addresses 列表,其中包含员工的地址列表。

我的代码设计如下

class Employee:
    Name=''
    Age =''
    Addresses =[]

class Address:
   State =''
   District =''
   City=''
adress1 =Address();
adress1.State='West bengal'
adress1.District ='Jalpaiguri'
adress1.City= 'Kolkata'

adress2 =Address()
adress2.State='West Bengal'
adress2.District ='Darjeeling'
adress2.City= 'Siliguri'
employee =Employee()
employee.name ='Gunjan'
employee.age =22
employee.Addresses.append(adress1)
employee.Addresses.append(adress2)

我想将员工转换为 json 表示。我试过了

json.dumps(employee.__dict__).

但这只是给我 Employee 的 name 和 age 属性,而不是它包含的地址列表。

如果有人能指导我获得完美的 json 表示形式,那将是非常有帮助的-

{"name":"Gunjan","Age":"22","Addresses" :[{"State":"West Bengal","City":"Siliguri","District":"Darjeeling" },{"州":"西孟加拉邦","城市":"加尔各答","区":"New Jalpaiguri"}]}

谢谢

【问题讨论】:

标签: python json


【解决方案1】:

实际上,您正在尝试创建一个可以转换为 JSON 的字典,而不是直接创建 JSON 表示(Python 允许这两种技术)。

要创建字典,试试这个:

class Employee(object):
    def __init__(self, name, age):
        self.name = name
        self.age = age
        self.addresses = []

    @property
    def __dict__(self):
        return {
            "name": self.name,
            "age": self.age,
            "addresses": [addr.__dict__ for addr in self.addresses]
        }


class Address(object):
    def __init__(self, state, district, city):
        self.state = state
        self.district = district
        self.city = city

employee = Employee("Gunjan", 22)
employee.addresses.append(Address("West Bengal", "Jalpaiguri", "Kolkata"))
employee.addresses.append(Address("West Bengal", "Darjeeling", "Siliguri"))

print employee.__dict__

它应该打印:

{'age': 22, 'name': 'Gunjan', 'addresses': [{'city': 'Kolkata', 'state': 'West Bengal', 'district': 'Jalpaiguri'}, {'city': 'Siliguri', 'state': 'West Bengal', 'district': 'Darjeeling'}]}

【讨论】:

    【解决方案2】:

    没什么,python中类成员的约定是小写的。

    另一个问题是您调用了emplyee.name,而您的成员是Name。

    如果你想在你的类中使用数据成员,你应该写 self.name 例如。

    如您所见,我实现了转储功能,如果一个类有对象列表,这是一个问题,我不知道任何直接的解决方案。

    我为你编写了符合你意图的代码。

    import json
    class Employee:
        def __init__(self):
            self.name=''
            self.age =''
            self.addresses =[]
    
        def dump(self):
            return {"Name": self.name,
                    "Age": self.age,
                    "Addresses": [addr.__dict__ for addr in self.addresses]}
    
    class Address:
        def __init__(self):
           self.state =''
           self.district =''
           self.city=''
    
    adress1 =Address()
    adress1.state='West bengal'
    adress1.district ='Jalpaiguri'
    adress1.city= 'Kolkata'
    
    adress2 =Address()
    adress2.state='West Bengal'
    adress2.district ='Darjeeling'
    adress2.city= 'Siliguri'
    
    employee =Employee()
    employee.name ='Gunjan'
    employee.age =22
    employee.addresses.append(adress1)
    employee.addresses.append(adress2)
    
    print json.dumps(employee.dump())
    

    结果:

    {"Age": 22, "Name": "Gunjan", "Addresses": [{"City": "", "State": "", "District": ""}, {"City": "Siliguri", "State": "West Bengal", "District": "Darjeeling"}]}
    

    【讨论】:

      猜你喜欢
      • 2015-03-23
      • 1970-01-01
      • 2014-01-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-30
      • 2011-08-19
      • 2011-11-09
      相关资源
      最近更新 更多