TypeError: JSON 对象必须是 str,而不是‘ byte’

我有以下抛出的非常基本的代码: TypeError: the JSON object must be str, not 'bytes'

import requests
import json


url = 'my url'
user = 'my user'
pwd = 'my password'


response = requests.get(url, auth=(user, pwd))


if(myResponse.ok):
Data = json.loads(myResponse.content)

我尝试将 decode 设置为 Data 变量,如下所示,但它会抛出同样的错误; jData = json.loads(myResponse.content).decode('utf-8')

有什么建议吗?

116323 次浏览

Let requests decode it for you:

data = response.json()

This will check headers (Content-Type) and response encoding, auto-detecting how to decode the content correctly.

json.loads(myResponse.content.decode('utf-8'))

You just put it in the wrong order, innocent mistake.


(In-depth answer). As courteously pointed out by wim, in some rare cases, they could opt for UTF-16 or UTF-32. These cases will be less common as the developers, in that scenario would be consciously deciding to throw away valuable bandwidth. So, if you run into encoding issues, you can change utf-8 to 16, 32, etc.

There are a couple of solutions for this. You could use request's built-in .json() function:

myResponse.json()

Or, you could opt for character detection via chardet. Chardet is a library developed based on a study. The library has one function: detect. Detect can detect most common encodings and then use them to encode your string with.

import chardet
json.loads(myResponse.content.decode(chardet.detect(myResponse.content)["encoding"]))

python3.6+ does this automatically.so your code shouldn't return error in python3.6+

what's new in python3.6