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

 

 

 

 

 

json.loads(json_data)報錯python

修改成json.loads(json_data.decode())便可json

一些修改成load什麼的方法會帶來新的報錯…this

直接添加decode()解決編碼

 

 

 

描述

Python decode() 方法以 encoding 指定的編碼格式解碼字符串。默認編碼爲字符串編碼。spa

語法

decode()方法語法:code

str.decode(encoding='UTF-8',errors='strict')

參數

  • encoding -- 要使用的編碼,如"UTF-8"。xml

  • errors -- 設置不一樣錯誤的處理方案。默認爲 'strict',意爲編碼錯誤引發一個UnicodeError。 其餘可能得值有 'ignore', 'replace', 'xmlcharrefreplace', 'backslashreplace' 以及經過 codecs.register_error() 註冊的任何值。blog

返回值

該方法返回解碼後的字符串。字符串

實例

如下實例展現了decode()方法的實例:string

實例(Python 3.0+)

#!/usr/bin/python str = "this is string example....wow!!!"; str = str.encode('base64','strict'); print "Encoded String: " + str; print "Decoded String: " + str.decode('base64','strict')

以上實例輸出結果以下:

Encoded String: dGhpcyBpcyBzdHJpbmcgZXhhbXBsZS4uLi53b3chISE= Decoded String: this is string example....wow!!!
相關文章
相關標籤/搜索