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的主要内容,如果未能解决你的问题,请参考以下文章
JSON解析时出现:TypeError: the JSON object must be str, bytes or bytearray, not NoneType
微信小程序解密 TypeError(the JSON object must be str, not ‘bytes')
Unalignable boolean Series provided as indexer (index of the boolean Series and of the indexed objec
RF之TypeError: Object of type 'bytes' is not JSON serializable
Python错误:TypeError:'str' does not support the buffer interface
如何克服 TypeError: cannot convert the series to <class 'float'> 错误