Easiest way to serialize a simple class object with simplejson?

前端 未结 7 2007
萌比男神i
萌比男神i 2020-12-07 15:55

I\'m trying to serialize a list of python objects with JSON (using simplejson) and am getting the error that the object \"is not JSON serializable\".

The class is a

相关标签:
7条回答
  • 2020-12-07 16:44

    This is kind of hackish and I'm sure there's probably a lot that can be wrong with it. However, I was producing a simple script and I ran the issue that I did not want to subclass my json serializer to serialize a list of model objects. I ended up using list comprehension

    Let: assets = list of modelobjects

    Code:

    myJson = json.dumps([x.__dict__ for x in assets])
    

    So far seems to have worked charmingly for my needs

    0 讨论(0)
提交回复
热议问题