Issue
Is there a more elegant way to parse a json file containing an array of elements?
Example:
import json
contents = ... # read json file
# 'elements_dict' is an array of dictionaries
elements_dict = json.loads(contents)
# 'elements' is a list of Element
elements = [Element(**element) for element in elements_dict]
Could the loads function somehow return an array of Element instead?
Solution
Solved the problem this way:
elements = json.loads(contents, object_hook=lambda d: Element(**d))
Answered By - codingjuanito
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.