【发布时间】:2020-05-02 03:10:48
【问题描述】:
我有一个歌曲类,它保存歌曲的属性,它是一个自定义类。我还有一个名为曲目列表的列表中的歌曲列表。当我尝试 json.dump 列表时,我收到一条错误消息:
TypeError: Object of type 'Song' is not JSON serializable
我将如何将此歌曲列表转换为 json?
这是返回错误的附加相关代码:
class Song:
def __init__(self, sname, sartist, coverart, albname, albartist, spotid):
self.sname = sname
self.sartist = sartist
self.coverart = coverart
self.albname = albname
self.albartist = albartist
self.spotid = spotid
tracklist = createDict(tracks) ##creates the list of songs, works fine
jsontracks = json.dumps(tracklist)
pp.pprint(jsontracks)
谢谢
【问题讨论】:
-
您能否发布数据样本?此外,您需要缩进 'def init...'。
-
这是解释in the documentation。参见
default参数的说明。 -
这能回答你的问题吗? How to make a class JSON serializable
标签: python json python-3.x class serialization