Serialize/Deserialize list of objects using json and python -
how serialize list of objects(person) json file , read json file , deserialize them objects? know how write json file i'm not clear on how convert objects json properly.
below simplified snippet of code. have list containing 2 people , want serialize them , save json file. deserialize them objects of type people.
thanks help.
import json class person(object): def __init__(self, name, nickname): self.name = name self.age = 0 self.nickname = nickname # create few people = person('john', 'joker') b = person('marisa', 'snickerdoodle') # add people list peeps = [] peeps.append(a) peeps.append(b) # dummy data saving json testing data = { 'name' : 'acme', 'shares' : 100, 'price' : 542.23 } open('data.json', 'w') outfile: json.dump(data, outfile)
the json
module expects lists, dicts, strings, numbers, booleans, , none, not custom classes. need make dictionary out of people
instance. simple way vars(a)
.
Comments
Post a Comment