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

时间:2017-12-26 13:07:49

标签: python json

我需要将多个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)

问题是什么,我该如何解决?

1 个答案:

答案 0 :(得分:0)

您的数组周围没有[]个元素 例如就像现在这样

array= '[], []' 

而不是

array= '[[], []]'

试试这个

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