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

时间:2018-11-05 14:59:56

标签: python json

好吧,所以我试图做一个字典重命名函数,但是无论我做什么,我都会不断收到错误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文件的样子,任何人有任何建议,我都会感谢他们

1 个答案:

答案 0 :(得分:0)

您使用的是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]}}}