TypeError: the JSON object must be str, bytes or bytearray, not dict
Posted Data+Science+Insight
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了TypeError: the JSON object must be str, bytes or bytearray, not dict相关的知识,希望对你有一定的参考价值。
TypeError: the JSON object must be str, bytes or bytearray, not dict
目录
TypeError: the JSON object must be str, bytes or bytearray, not dict
问题:
import json
dic = 'a':123, 'b':"456", 'c':"liming"
# dic_str = json.loads(str(dic).replace("'", "\\""))
dic_str = json.loads(dic)
print(dic_str)
解决:
单引号的字符串不符合Json的标准格式所以再次使用了 replace("'", "\\"")
import json
dic = 'a':123, 'b':"456", 'c':"liming"
dic_str = json.loads(str(dic).replace("'", "\\""))
# dic_str = json.loads(dic)
print(dic_str)
完整错误:
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-402-239df5f3d40d> in <module> 2 dic = 'a':123, 'b':"456", 'c':"liming" 3 # dic_str = json.loads(str(dic).replace("'", "\\"")) ----> 4 dic_str = json.loads(dic) 5 print(dic_str) D:\\anaconda\\lib\\json\\__init__.py in loads(s, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw) 339 else: 340 if not isinstance(s, (bytes, bytearray)): --> 341 raise TypeError(f'the JSON object must be str, bytes or bytearray, ' 342 f'not s.__class__.__name__') 343 s = s.decode(detect_encoding(s), 'surrogatepass') TypeError: the JSON object must be str, bytes or bytearray, not dict
以上是关于TypeError: the JSON object must be str, bytes or bytearray, not dict的主要内容,如果未能解决你的问题,请参考以下文章