python错误: TypeError: the JSON object must be str, bytes or bytearray, not 'dict'解决办法
2018-07-14 21:46
3263 查看
当我尝试运行以下代码,来练习使用json.loads()和json.dumps()函数时,系统给我报出了这个错误。
import json text = {"a":1,"b":2,"c":3} j = json.loads(text) print(j)
Traceback (most recent call last): File "test.py", line 5, in <module> j = json.loads(text) File "C:\Users\Reborn\AppData\Local\Programs\Python\Python36-32\lib\json\__init__.py", line 348, in loads 'not {!r}'.format(s.__class__.__name__)) TypeError: the JSON object must be str, bytes or bytearray, not 'dict'其实解决办法很简单,由于text现在是一个字典,只需要用'''符号将它转换成字符串就可以了。但我们有必要知道loads()和jumps()这两个函数的具体用法,接下来我就简单说一下。
loads()
loads(param)是将文本字符串转换为json对象的函数,其函数名是load string 的缩写,意思是加载字符串。所以其参数param必须要是一个字典型的字符串。且字典的键必须用双引号来包裹。
dumps()
dumps(param)是将json数据对象转换为文本字符串的函数,其函数名是dump string 的缩写,意思是输出字符串,所以其参数param必须要是json对象,也就是loads()函数返回的数据类型。
阅读更多相关文章推荐
- Python之Headers value 1 must be of type str or bytes, not <class 'int'>错误的解决
- Python 读写文件 中文乱码 错误TypeError: write() argument must be str, not bytes+
- Python 读写文件 中文乱码 错误TypeError: write() argument must be str, not bytes+
- Python 读写文件 错误TypeError: write() argument must be str, not bytes+
- python2.7web代码用python3.6运行时,浏览器发送请求报错TypeError: Object of type 'bytes' is not JSON serializable的解决办法
- Python错误:TypeError: 'int' object is not callable解决办法
- python2.7 使用super关键词 报错 TypeError: must be type, not classobj 解决办法
- python3中,socket使用send函数时出现的错误:TypeError: a bytes-like object is required, not 'str'
- Python 3中套接字编程中遇到TypeError: 'str' does not support the buffer interface的解决办法
- Python 3.5 Socket TypeError: a bytes-like object is required, not 'str' 错误提示
- TypeError: the JSON object must be str, not 'bytes'
- Python 3中套接字编程中遇到TypeError: 'str' does not support the buffer interface的解决办法
- python32版本,出现问题“struct.error: argument for 's' must be a bytes object”的解决办法
- Python 3中套接字编程中遇到TypeError: 'str' does not support the buffer interface的解决办法
- python write()参数错误,导致报错TypeError: a bytes-like object is required, not 'str'
- TypeError: strptime() argument 0 must be str, not class 'bytes'解决办法
- Python 出现错误TypeError: ‘NoneType’ object is not iterable解决办法
- Python 3中套接字编程中遇到TypeError: 'str' does not support the buffer interface的解决办法
- Python 3中套接字编程中遇到TypeError: 'str' does not support the buffer interface的解决办法
- python2.7 使用super关键词 报错 TypeError: must be type, not classobj 解决办法