簡體   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