抛出json.dump"TypeError:{...}不可JSON序列化"在看似有效的物体上? [英] json.dump throwing "TypeError: {...} is not JSON serializable" on seemingly valid object?

查看:231
本文介绍了抛出json.dump"TypeError:{...}不可JSON序列化"在看似有效的物体上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景:我正在编写一个Python程序,该程序可以管理我的音乐文件.它将爬网目录并将文件和它们的元数据(通过mutagen)(通过mutagen)放入JSON中,作为简单的数据库".我的目录可以很好地搜索,但是当我尝试保存数据库或将其编码为JSON时,它将引发"TypeError:{...}不可序列化JSON" (...是字典中的一些键和值,更多内容请参见下文)

Background: I am writing a python program which should manage my music files. It crawls directories and puts the files and their meta data (via mutagen), encoded in JSON, in a file as a simple "database". I have the directory searching fine, but when I try and save the database, or encode to JSON, it throws a "TypeError: {...} is not JSON serializable" (the ... are some keys and values from a dict, more on that below)

问题:程序按照以下格式构建大型词典对象:

The Problem: The program builds a large dictionary object following this format:

{
    "<song id>":{
        "artist":"<song artist>",
        "album":"<song album>",
        "title":"<song title>"},
    ...
}

每个单首歌曲文件都通过此格式编制索引.当我尝试将数据库转储到文件时,得到以下信息:

Every single song file is indexed via this format. When I try to dump the database to a file, I get this:

Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    sit()
  File "D:\workbench\ideas\musicmanager\v0\spider.py", line 116, in sit
    json.dump(js.db,f,True)
  File "C:\Python27\lib\json\__init__.py", line 181, in dump
    for chunk in iterable:
  File "C:\Python27\lib\json\encoder.py", line 428, in _iterencode
    for chunk in _iterencode_dict(o, _current_indent_level):
  File "C:\Python27\lib\json\encoder.py", line 402, in _iterencode_dict
    for chunk in chunks:
  File "C:\Python27\lib\json\encoder.py", line 402, in _iterencode_dict
    for chunk in chunks:
  File "C:\Python27\lib\json\encoder.py", line 436, in _iterencode
    o = _default(o)
  File "C:\Python27\lib\json\encoder.py", line 178, in default
    raise TypeError(repr(o) + " is not JSON serializable")
TypeError: {'album': [u"Rooney's Lost Album"], 'title': [u'The Kids
After Sunset'], 'artist': [u'Rooney']} is not JSON serializable

该特定歌曲条目的键为

Rooney|Rooney's Lost Album|The Kids After Sunset|The Kids After Sunset.itunes.mp3

(ID的格式有点笨重,我可能最终会对其进行散列...)

(the format for the id is a little bulky, I might end up hashing that...)

所以我试图

json.dumps({'album': [u"Rooney's Lost Album"], 'title': [u'The Kids
After Sunset'], 'artist': [u'Rooney']})

效果很好,

json.dumps({"Rooney|Rooney's Lost Album|The Kids After Sunset|The Kids
After Sunset.itunes.mp3":""})

然后我尝试了这个:

rooney = "Rooney|Rooney's Lost Album|The Kids After Sunset|The Kids
    After Sunset.itunes.mp3"
json.dumps({rooney:js.db['songsbyid'][rooney]})

哪些再次失败,并键入错误.

Which failed with the type error again.

为什么该对象因json.dump而失败?我还有许多其他对象,它们的键包含管道"|"和撇号'" ...目前,我没有其他人可以对此进行测试,我应该发布数据库对象的腌制版本吗?

Why does that object fail with json.dump? I have plenty of other objects with keys containing pipes "|" and apostrophes "'"... At the moment, I have no way for anyone else to test this, should I post a pickled version of the database object?

附加说明

  • json.dumps下面的结果对象很好,所以我想知道问题是否与数据库的大小有关?

  • The resulting object below json.dumps is fine, so I am wondering if the issue has to do with the size of the database in any way?

{rooney:js.db ['songsbyid'] [rooney]} {鲁尼|鲁尼的失落专辑|日落之后的孩子|孩子们 在Sunset.itunes.mp3之后:{'album':[u鲁尼的失落专辑"]], 'title':[u'日落之后的孩子'],'artist':[u'Rooney']}}

{rooney:js.db['songsbyid'][rooney]} {"Rooney|Rooney's Lost Album|The Kids After Sunset|The Kids After Sunset.itunes.mp3": {'album': [u"Rooney's Lost Album"], 'title': [u'The Kids After Sunset'], 'artist': [u'Rooney']}}

  • 如果我通过重命名扩展名来排除歌曲,从而使脚本将其忽略,则另一首任意歌曲会导致相同的错误.我重命名并排除了这首新歌,然后碰到另一首新歌...我不知道有多少首.

  • If I exclude the song by renaming the extension so the script ignores it, another arbitrary song causes the same error. I renamed&excluded this new song, and ran into ANOTHER new song... I don't know how many there are.

    我更改了程序,以爬网包含原始问题歌曲的下一个最远子目录,并且json.dump在另一首完全不同的歌曲上引发TypeError ...

    I changed my program to crawl the next furthest sub-directory containing the original problem song, and json.dump raised a TypeError on a completely different song...

    推荐答案

    因为它实际上不是字典;这是另一种看起来像字典的映射类型.使用type()进行验证.将其传递给dict()可以从中获得真实的字典.

    Because it's not actually a dictionary; it's another mapping type that looks like a dictionary. Use type() to verify. Pass it to dict() to get a real dictionary from it.

    这篇关于抛出json.dump"TypeError:{...}不可JSON序列化"在看似有效的物体上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆