多数组JSON作为字符串转换为数组python

2024-10-02 08:19:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要将多个JSON数组作为字符串处理,并转换为Python中的列表。以下是我尝试过的:

array = '[{"drinks": ["coffee", "tea", "water"]}],' \
            '[{"drinks": ["coffee", "tea", "water"]}]'
data = json.loads(array)
print(data)

这将生成以下错误:

Traceback (most recent call last):   File
   "C:/Users/aessam/Desktop/sen/josnre.py", line 10, in <module>
       data = json.loads(array)   File "C:\Users\aessam\AppData\Local\Programs\Python\Python35\lib\json\__init__.py",
   line 319, in loads
       return _default_decoder.decode(s)   File "C:\Users\aessam\AppData\Local\Programs\Python\Python35\lib\json\decoder.py",
   line 342, in decode
       raise JSONDecodeError("Extra data", s, end) json.decoder.JSONDecodeError: Extra data: line 1 column 41 (char 40)

有什么问题,我该怎么解决?你知道吗


Tags: inpyjsondatalinearrayusersfile
1条回答
网友
1楼 · 发布于 2024-10-02 08:19:57

数组中没有[]个元素 e、 现在是这样的

array= '[], []' 

而不是

array= '[[], []]'

试试这个

array = '[[{"drinks": ["coffee", "tea", "water"]}],' \
            '[{"drinks": ["coffee", "tea", "water"]}]]'
data = json.loads(array)
print(data)

相关问题 更多 >

    热门问题