I struggled with the issue when appending objects into a list in Python. After retrieving JSON file, it assigns each value into House object and appends it into houses list. However, after appending the second object, it overwrites the first object in the list. Could you give me help?
jsonfile.json
"features": [
{
"properties": {
"id": "ea299c1e-2bbb-5a97-bb94-befe384f0eb2",
"name": "Albert"
},
{
"properties": {
"id": "47200ba4-db80-56fa-99ad-c4abe7296376",
"name": "Batman"
}
python.py
class House:
def __init__(self, id, name):
self.id = id
self.name=name
houses =[]
......
with open('./data/jsonfile.json', 'r') as myfile:
data = myfile.read()
obj = json.loads(data)['features']
for o in obj:
print(o['properties']['id'])
House.id = o['properties']['id']
House.name = o['properties']['name]
print(House.id)
houses.append(House)
print("----------------------")
print("----------------------")
print(houses[0].id)
print(houses[1].id)
print(len(houses))
console
ea299c1e-2bbb-5a97-bb94-befe384f0eb2
ea299c1e-2bbb-5a97-bb94-befe384f0eb2
----------------------
47200ba4-db80-56fa-99ad-c4abe7296376
47200ba4-db80-56fa-99ad-c4abe7296376
----------------------
----------------------
47200ba4-db80-56fa-99ad-c4abe7296376. <--- It should be 'ea299c1e-2bbb-5a97-bb94-befe384f0eb2'
47200ba4-db80-56fa-99ad-c4abe7296376
2