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

相关推荐
陈天伟教授21 小时前
人工智能训练师认证教程(2)Python os入门教程
前端·数据库·python
2301_764441331 天前
Aella Science Dataset Explorer 部署教程笔记
笔记·python·全文检索
爱笑的眼睛111 天前
GraphQL:从数据查询到应用架构的范式演进
java·人工智能·python·ai
BoBoZz191 天前
ExtractSelection 选择和提取数据集中的特定点,以及如何反转该选择
python·vtk·图形渲染·图形处理
liwulin05061 天前
【PYTHON-YOLOV8N】如何自定义数据集
开发语言·python·yolo
生产队队长1 天前
Web:免费的JSON接口
json
木头左1 天前
LSTM量化交易策略中时间序列预测的关键输入参数分析与Python实现
人工智能·python·lstm
电子硬件笔记1 天前
Python语言编程导论第七章 数据结构
开发语言·数据结构·python
HyperAI超神经1 天前
【vLLM 学习】Prithvi Geospatial Mae
人工智能·python·深度学习·学习·大语言模型·gpu·vllm
逻极1 天前
Python MySQL防SQL注入实战:从字符串拼接的坑到参数化查询的救赎
python·mysql·安全·sql注入