I have a class called Data like this:
class Data:
def __init__(self, ticker, comments, submissions):
self.ticker = ticker
self.comments = comments
self.submissions = submissions
Where ticker is a string, comments is a list of objects of type Comment and submissions is a list of objectf of type Submission. Comment and Submission have their own fields.
Now I have a list of objects of type Data
I want to itrate through the list and get a JSON String containing all the elements and print it to a file.
My code:
json_string = json.dumps([ob.__dict__ for ob in data_list])
f = open("data.json", "w")
f.write(json_string)
f.close()
This throws an error of type:
TypeError: Object of type Comment is not JSON serializable
I can't figure out what I'm doing wrong here, does anyone know?
Edit:
Comment class:
class Comment:
def __init__(self, author_name, body, ups):
self.author_name = author_name
self.body = body
self.ups = ups
All fields are string/int
Commentlook like?