2017-12-26 21 views
0

複数のJSON配列を文字列として処理し、Pythonのリストに変換する必要があります。ここで私が試したものです:複数配列のJSONを文字列として配列に変換する

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) 

が問題何だし、どのように私はそれを修正することができますか?

答えて

0

あなたの配列には、要素 の周りに[]がありません。それがこの

array = '[[{"drinks": ["coffee", "tea", "water"]}],' \ 
      '[{"drinks": ["coffee", "tea", "water"]}]]' 
data = json.loads(array) 
print(data) 
を試してみて、この現在

array= '[], []' 

代わり

array= '[[], []]' 

のようなものです

関連する問題