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

简介: 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’

通过如下代码可复现问题

>>> 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,则不会出现以上问题。

>>> 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

相关文章
|
7月前
|
Python
str'object is not callable
str'object is not callable
210 1
|
7月前
|
数据安全/隐私保护 Python
TypeError: a bytes-like object is required, not ‘str‘,如何解决?
TypeError: a bytes-like object is required, not ‘str‘,如何解决?
66 0
|
8月前
|
JSON Java 应用服务中间件
TypeToken分析(json字符串- list对象)
TypeToken分析(json字符串- list对象)
71 0
|
8月前
|
JSON 数据格式
TypeError: Object of type ‘float32‘ is not JSON serializable
TypeError: Object of type ‘float32‘ is not JSON serializable
129 0
|
8月前
|
Python
Python3 ‘str‘ object has no attribute ‘decode‘. Did you mean: ‘encode‘?
Python3 ‘str‘ object has no attribute ‘decode‘. Did you mean: ‘encode‘?
171 0
|
11月前
|
Python
Python 报错AttributeError: ‘str‘ object has no attribute ‘decode‘解决办法
Python 报错AttributeError: ‘str‘ object has no attribute ‘decode‘解决办法
|
11月前
|
JSON 数据格式
Json解析问题—数组用Gson反序列化为List双引号消失
现在的目的是这样的,我想从一串Object类型的字符串,转化成对应的实体类对象。 简单来说就是把这个字符串给一个个独立分开。 我先写出实体类对象和Json的数据,看完再开始解析。
106 0
|
JSON 关系型数据库 MySQL
MySQL:JSON字段List成员检查
MySQL:JSON字段List成员检查
80 0
|
1月前
|
存储 JSON Apache
揭秘 Variant 数据类型:灵活应对半结构化数据,JSON查询提速超 8 倍,存储空间节省 65%
在最新发布的阿里云数据库 SelectDB 的内核 Apache Doris 2.1 新版本中,我们引入了全新的数据类型 Variant,对半结构化数据分析能力进行了全面增强。无需提前在表结构中定义具体的列,彻底改变了 Doris 过去基于 String、JSONB 等行存类型的存储和查询方式。
揭秘 Variant 数据类型:灵活应对半结构化数据,JSON查询提速超 8 倍,存储空间节省 65%
|
3天前
|
存储 JSON 数据处理