通过遍历JSON数组创建嵌套类实例

时间:2016-09-17 15:05:05

标签: python arrays json class nested

此处的代码位于:https://github.com/mooperd/json-mangling

我试图在python中教自己嵌套类数组的机制,但是我遇到了这个问题。我不明白为什么我不能使用:

network.subnets.subnets.append(subnet_2)

如果没有这一行,代码似乎工作正常,但当然缺少第三级。如果我想进一步添加嵌套,如何实现?

我正在寻找的输出是一堆嵌套的Network()类实例。

我有一些数据:

[
    {
        "name": "bob",
        "cidr": "10.141.0.0/16",
        "subnets": [
            {
                "name": "grace",
                "cidr": "10.141.1.0/24",
                "subnets": [
                    {
                        "name": "pogo",
                        "cidr": "10.141.1.0/25",
                        "subnets": []
                    },
                    {
                        "name": "mungo",
                        "cidr": "10.141.2.0/24",
                        "subnets": []
                    }
                ]
            },
            {
                "name": "mary",
                "cidr": "10.141.2.0/24",
                "subnets": []
            },
            {
                "name": "sophie",
                "cidr": "10.141.3.0/24",
                "subnets": []
            }
        ]
    },
    {
        "name": "barbera",
        "cidr": "10.141.0.0/16",
        "subnets": [
            {
                "name": "chris",
                "cidr": "10.141.1.0/24",
                "subnets": []
            },
            {
                "name": "david",
                "cidr": "10.141.2.0/24",
                "subnets": []
            },
            {
                "name": "logan",
                "cidr": "10.141.3.0/24",
                "subnets": []
            }
        ]
    }
]

我有一个模特:

class Network(object):
    def __init__(self, name):
        self.name = name
        self.cidr = ""
        self.subnets = []

    def __repr__(self):
        return "Network(name={}, cidr={}, subnets={})".format(
            self.name,
            self.cidr,
            self.subnets,
        )

这个功能:

def load_networks(networks_dict):
        network_list = []
        for network_dict in networks_dict:
            network = Network(network_dict["name"])
            network.cidr = network_dict["cidr"]
            network.subnets = list()
            if "subnets" in network_dict:
                for subnet_dict_1 in network_dict["subnets"]:
                    subnet_1 = Network(subnet_dict_1["name"])
                    subnet_1.cidr = subnet_dict_1["cidr"]
                    subnet_1.subnets = list()

                    if "subnets" in subnet_dict_1:
                        for subnet_dict_2 in subnet_dict_1["subnets"]:
                            subnet_2 = Network(subnet_dict_2["name"])
                            subnet_2.cidr = subnet_dict_2["cidr"]
                            subnet_2.subnets = list()

                            network.subnets.subnets.append(subnet_2)

                    network.subnets.append(subnet_1)

            network_list.append(network)

        return network_list

但是这给我一个错误:

~/dev/json-mangling $python mangle.py 
Traceback (most recent call last):
  File "mangle.py", line 43, in <module>
    foo = load_networks(json_file)
  File "mangle.py", line 33, in load_networks
    network.subnets.subnets.append(subnet_2)
AttributeError: 'list' object has no attribute 'subnets'

0 个答案:

没有答案