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


 

 

 

 

 

json.loads(json_data)報錯

修改為json.loads(json_data.decode())即可

一些修改為load什么的方法會帶來新的報錯…

直接添加decode()解決

 

 

 

描述

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

語法

decode()方法語法:

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

參數

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

  • errors -- 設置不同錯誤的處理方案。默認為 'strict',意為編碼錯誤引起一個UnicodeError。 其他可能得值有 'ignore', 'replace', 'xmlcharrefreplace', 'backslashreplace' 以及通過 codecs.register_error() 注冊的任何值。

返回值

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

實例

以下實例展示了decode()方法的實例:

實例(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!!!

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM