繁体   English   中英

类型为'_NestedList'的python&livejson对象不是JSON可序列化的

[英]python & livejson Object of type '_NestedList' is not JSON serializable

好的,所以我试图做一个字典重命名函数,但是我一直收到错误消息Object of type '_NestedList' is not JSON serializable无论我做什么, Object of type '_NestedList' is not JSON serializable ,我已经尝试了一些方法,当我尝试将它们替换时,它们可以工作没问题...除非我要尝试一下。

它的json看起来像custom["commands"]["command"]["beep"]
所以我想做的就是将其更改为custom["commands"]["command"]["boom"]

{
    "command": {},
    "commands": {
        "command": {
            "beep": {
                "created": "2018-11-04 16:32:50.013260",
                "created2": 1541349170.0132835,
                "createdby": "me",
                "disablefor": [],
                "enabledfor": [],
                "message": "asd",
                "public": "self",
                "type": "text",
                "unsendtimer": 0,
                "unsendtrigger": false
            },
            "bep": {
                "created": "2018-11-04 16:34:38.723840",
                "created2": 1541349278.7238638,
                "createdby": "me",
                "disablefor": [],
                "enabledfor": [],
                "message": "asd",
                "public": "self",
                "type": "text",
                "unsendtimer": 0,
                "unsendtrigger": false
            },
            "boop": {
                "STKID": "423",
                "STKPKGID": "1",
                "STKVER": "100",
                "created": "2018-10-27 00:53:38.067740",
                "created2": 1540601618.0677645,
                "createdby": "me",
                "disablefor": [
                    "u69a0086845f2d38c5ecfd91a7601f3c1",
                    "ua2ed27b7932f647b492daa68ef33c0cc"
                ],
                "enabledfor": [],
                "message": "8775249726676",
                "public": "on",
                "type": "sticker",
                "unsendtimer": 0,
                "unsendtrigger": true
            }
        },
        "commandgrab": false,
        "commandgroup": ""
    }
}

这就是json文件的样子,任何人有任何建议,我都会爱上他们,谢谢

您正在使用livejson包,而不是python的内置json包。

livejson json结构是livejson对象的树; 显然livejson不直接支持在键之间移动这些对象:

>>> import livejson
>>> test['foo'] = {'bar': {'baz': [1, 2, 3]}
>>> test
{'foo': {'bar': {'baz': [1, 2, 3]}}}

>>> type(test['foo']['bar']['baz'])
<class 'livejson._NestedList'>


>>> test['foo']['bar']['quux'] = test['foo']['bar']['baz']
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  ...
TypeError: Object of type _NestedList is not JSON serializable

livejson对象具有data属性,该属性返回标准python列表和livejson包装的字典,因此您需要使用此属性来重新分配键的值:

>>> test['foo']['bar']['quux'] = test['foo']['bar']['baz'].data

# Now remove the old value
>>> del test['foo']['bar']['baz']
>>> test

{'foo': {'bar': {'quux': [1, 2, 3]}}}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM