Mam plik JSON zawierający regiony NZ i miasta: {"Region": ["Region_name": "Northland", "miasta": [& ...

0
Hooman Bahreini 15 marzec 2021, 07:55

1 odpowiedź

Najlepsza odpowiedź

Szukasz czegoś takiego

d = {
    "region": [
        {
            "region_name": "northland",
            "towns": [
                "whangarei",
                "paihia",
                "russel"
            ]
        },
        {
            "region_name": "auckland",
            "towns": [
                "auckland",
                "porirua"
            ]
        }
    ]
}

class Region:
    def __init__(self, region_name, towns):
        self.region_name = region_name
        self.towns = towns
        
d_deserialized = {"region":[]}
for each_region in d["region"]:
    d_deserialized["region"].append(Region(each_region["region_name"], each_region["towns"]))
    
print(d_deserialized)
{'region': [<__main__.Region object at 0x0000022344DB3DF0>, <__main__.Region object at 0x0000022344DB3D30>]}
1
Epsi95 15 marzec 2021, 05:03