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

相关推荐
jerryinwuhan1 小时前
Python快速入门(纯净版)
python
liferecords1 小时前
第 12 讲 · Python 侧接入:火焰图定位热点与间接使用硬件加速
python·性能分析·鲲鹏·火焰图·ctypes
hhzz1 小时前
【OpenCV 入门到精通 11】机器学习应用:KNN、SVM 与 K-Means 实战
人工智能·python·深度学习·opencv
K 旺仔小馒头1 小时前
【项目】商城系统用户端测试报告
自动化测试·python·功能测试
东莞市云毅网络有限公司1 小时前
用 SQLite FTS5 给企业文档建本地全文索引:中文分词与权重调优
python·数据清洗·rag·企业知识库·文档解析
钱栈up1 小时前
health从200变000:一次差点引发双端口冲突的后端巡检复盘
python
正经教主1 小时前
【FDE系列】阶段2:Day 22:变量、数据类型、条件判断 — Python 的“记忆“和“判断“
人工智能·python·fde
打工仔折腾 AI2 小时前
Pascal Editor 本地部署实战:Bun 启动 WebGPU 3D 编辑器并解决公网访问报错
人工智能·后端·python·性能优化
m0_547486662 小时前
《Python从入门到数据分析应用》全套PPT课件2026
python·数据分析
All for pursuit.2 小时前
FastAPI+Ollama 部署本地AI大模型对话助手
python·github·aigc·ollama