Skip to content
Advertisement

Python Correctly Parse a Complex Object into a JSON format

I have the following which I’d like to parse it into JSON. The class has a list of item object also

JavaScript

So, when I try to parse the instance of Item class, root.reprJSON() I get the following result.

JavaScript

But I’d like to get the values of those item also into a single json object. I don’t know how to do it, would appreciate any help. Thank you

Edit

Following code create an instance of item class and filled it with data.

JavaScript

Advertisement

Answer

Looking at your code you can use this demo solution in your code as I’m storing objects of Demo class in the Items list. You need to write serialize() and dumper() methods in Items class, and also changes need to be done in reprJSON method for iteration on Items list.

JavaScript

Output:

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement