python JSON object must be str, bytes or bytearray, not 'dict

后端 未结 3 1279
梦如初夏
梦如初夏 2020-12-07 17:29

In Python 3, to load json previously saved like this:

json.dumps(dictionary)

the output is something like

{\"(\'Hello\',)\": 6, \"

3条回答
  •  北海茫月
    2020-12-07 18:15

    json.loads take a string as input and returns a dictionary as output.

    json.dumps take a dictionary as input and returns a string as output.


    With json.loads({"('Hello',)": 6, "('Hi',)": 5}),

    You are calling json.loads with a dictionary as input.

    You can fix it as follows (though I'm not quite sure what's the point of that):

    d1 = {"('Hello',)": 6, "('Hi',)": 5}
    s1 = json.dumps(d1)
    d2 = json.loads(s1)
    

提交回复
热议问题