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

相关推荐
fengkai45458 分钟前
Python运维实战
运维·开发语言·python·ssh·sftp·paramiko
用户454284510628134 分钟前
一张 3000×2000 的 PSD 为什么 86MB:从体积构成到上传墙
python
用户298698530141 小时前
使用 Python 提取 Word 文档中的文本与图片
后端·python·api
用户0332126663672 小时前
使用 Python 在 PDF 中创建表单域
python
羲云网络12 小时前
GPT Image 2.6 海报改横竖版:文字安全区、排版分离与交付验收
图像处理·人工智能·python·aigc
曲径海盗13 小时前
【Python 学习第 65 天】字典基础
python·学习笔记·编程入门
Wellijohn13 小时前
Docker 瘦身、手写 MCP、异步并发:被问得最多的 12 个问题,一次答完
python·docker·mcp
VW_40416 小时前
csdn-2026-11-23-第一站踩坑
爬虫·python
yiangL_17 小时前
课后作业4:基于 Python 的古诗词词云可视化(P205)
开发语言·python
Dmz丶17 小时前
前端转 AI 100 天 Day 52:RAG 项目实战(一)——从零搭建企业知识库
python