TypeError the JSON object must be str, bytes or bytearray, not ‘list‘

在使用python的jason库时,偶然碰到以下问题

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

通过如下代码可复现问题

python 复制代码
>>> a
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> import json
>>> ra = json.loads(a) 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "D:\Python36\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 'list'

分析可知,python中的列表如果要通过json库解析为jason对象,就会出现以上提示。意思是,jason的对象必须是字符串,字节或字节数组,不能是列表。如果将 a 通过 str(a),在调用 loads,则不会出现以上问题。

python 复制代码
>>> a   
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> str(a)                 
'[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]'
>>> json.loads(str(a)) 
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

》扩展说明 》

jason导入数据

jason.load

jason.loads 其中s表示字符串

jason 导出数据

f = open('./data/test.txt', 'r')

jason.dump(data, f)

jason.dumps

相关推荐
u***324341 分钟前
使用python进行PostgreSQL 数据库连接
数据库·python·postgresql
青瓷程序设计3 小时前
动物识别系统【最新版】Python+TensorFlow+Vue3+Django+人工智能+深度学习+卷积神经网络算法
人工智能·python·深度学习
tobebetter95273 小时前
How to manage python versions on windows
开发语言·windows·python
F_D_Z4 小时前
数据集相关类代码回顾理解 | sns.distplot\%matplotlib inline\sns.scatterplot
python·深度学习·matplotlib
daidaidaiyu4 小时前
一文入门 LangGraph 开发
python·ai
不知更鸟5 小时前
前端报错:快速解决Django接口404问题
前端·python·django
4***72135 小时前
【玩转全栈】----Django模板语法、请求与响应
数据库·python·django
梁正雄6 小时前
1、python基础语法
开发语言·python
ituff7 小时前
微软认证考试又免费了
后端·python·flask
梁正雄8 小时前
2、Python流程控制
开发语言·python