python JSON 对象必须是 str、bytes 或 bytearray,而不是 'dict

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42354001/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 21:36:43  来源:igfitidea点击:

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

pythonjsondictionary

提问by dila93

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

在 Python 3 中,要加载以前保存的 json,如下所示:

json.dumps(dictionary)

json.dumps(dictionary)

the output is something like

输出是这样的

{"('Hello',)": 6, "('Hi',)": 5}

{"('Hello',)": 6, "('Hi',)": 5}

when I use

当我使用

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

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

it doesn't works, this happens:

它不起作用,发生这种情况:

TypeError: the JSON object must be str, bytes or bytearray, not 'dict'

类型错误:JSON 对象必须是 str、bytes 或 bytearray,而不是 'dict'

回答by barak manos

json.loadstake a string as input and returns a dictionary as output.

json.loads将一个字符串作为输入并返回一个字典作为输出。

json.dumpstake a dictionary as input and returns a string as output.

json.dumps以字典作为输入并返回一个字符串作为输出。



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

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

You are calling json.loadswith a dictionary as input.

您正在json.loads使用字典作为输入进行调用。

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)

回答by Mark Reed

You are passing a dictionary to a function that expects a string.

您正在将字典传递给需要字符串的函数。

This syntax:

此语法:

{"('Hello',)": 6, "('Hi',)": 5}

is both a valid Python dictionary literal and a valid JSON object literal. But loadsdoesn't take a dictionary; it takes a string, which it then interprets as JSON and returns the result asa dictionary (or string or array or number, depending on the JSON, but usually a dictionary).

既是有效的 Python 字典字面量又是有效的 JSON 对象字面量。但loads不带字典;它接受一个字符串,然后将其解释为 JSON 并将结果作为字典(或字符串、数组或数字,取决于 JSON,但通常是字典)返回。

If you pass this string to loads:

如果您将此字符串传递给loads

'''{"('Hello',)": 6, "('Hi',)": 5}'''

then it will return a dictionary that looks a lot like the one you are trying to pass to it.

然后它会返回一个看起来很像你试图传递给它的字典。

You could also exploit the similarity of JSON object literals to Python dictionary literals by doing this:

您还可以通过执行以下操作来利用 JSON 对象文字与 Python 字典文字的相似性:

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

But in either case you would just get back the dictionary that you're passing in, so I'm not sure what it would accomplish. What's your goal?

但是在任何一种情况下,您都只会取回您传入的字典,所以我不确定它会完成什么。你的目标是什么?

回答by Laxman Jeergal

import json
data = json.load(open('/Users/laxmanjeergal/Desktop/json.json'))
jtopy=json.dumps(data) #json.dumps take a dictionary as input and returns a string as output.
dict_json=json.loads(jtopy) # json.loads take a string as input and returns a dictionary as output.
print(dict_json["shipments"])