2014-03-07 76 views
1

我正在使用Python 2.7.6來解析JSON文件,但出現錯誤,我不確定爲什麼。這是我第一次處理Python,所以這可能是一個非常基本的問題,但我看起來有點堆棧溢出,似乎無法弄清楚什麼是錯的。Python中的JSON解析錯誤

這裏是我的Python代碼來解析數據:

import json 
from pprint import pprint 

with open ('test.json') as data_file: 
    data = json.load(data_file); 
pprint(data) 

,這裏是我的JSON文件:

{"votes": {"funny": 0, "useful": 0, "cool": 0}, "user_id": "hckr9Hf8BUHcXfOSDv9eJA", "review_id": "K6EEJo0I8AbwGWvwe5SJYQ", "stars": 5, "date": "2013-05-05", "text": "This place is fantastic. they have a restaurant inside the grocery store. very good food.", "type": "review", "business_id": "uPezkdNi_x_SwWlf_2rcMw"} 
{"votes": {"funny": 0, "useful": 0, "cool": 1}, "user_id": "PK3TxomYLwZuOXonmYqjNw", "review_id": "5ivy-tczAQ4WYrmVF6YoKg", "stars": 5, "date": "2013-08-11", "text": "This is going to be a place we go back to many times!", "type": "review", "business_id": "UB2j_EV3CIM_E4LcpadKMQ"} 

這是我的錯誤,當我解析JSON:

File "./parse.py", line 6, in <module> 
    data = json.load(data_file); 
    File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 290, in load 
    **kw) 
    File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 338, in loads 
    return _default_decoder.decode(s) 
    File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 368, in decode 
    raise ValueError(errmsg("Extra data", s, end, len(s))) 
ValueError: Extra data: line 2 column 1 - line 3 column 1 (char 318 - 600) 

奇怪的是,如果我只有JSON的第一行,我可以成功解析數據。任何幫助理解錯誤並幫助我解決它將不勝感激。

+0

您希望在data_file中使用行 - 或者在行尾加括號括號'[]'和逗號。 –

+0

在頂層,有效的JSON文件*必須是*單個*對象'{...}'或單個*數組''[...]';你不能有兩個對象背靠背。 –

回答

3

這不是有效的JSON - 你不能像這樣粘貼兩個哈希值......嘗試類似的東西。你會注意到我之間放置了一個逗號,並將整個哈希集放在一個數組中。

[ 
    { 
     "stars" : 5, 
     "date" : "2013-05-05", 
     "review_id" : "K6EEJo0I8AbwGWvwe5SJYQ", 
     "text" : "This place is fantastic. they have a restaurant inside the grocery store. very good food.", 
     "user_id" : "hckr9Hf8BUHcXfOSDv9eJA", 
     "type" : "review", 
     "votes" : { 
     "funny" : 0, 
     "cool" : 0, 
     "useful" : 0 
     }, 
     "business_id" : "uPezkdNi_x_SwWlf_2rcMw" 
    }, 
    { 
     "stars" : 5, 
     "date" : "2013-08-11", 
     "review_id" : "5ivy-tczAQ4WYrmVF6YoKg", 
     "text" : "This is going to be a place we go back to many times!", 
     "user_id" : "PK3TxomYLwZuOXonmYqjNw", 
     "type" : "review", 
     "votes" : { 
     "funny" : 0, 
     "cool" : 1, 
     "useful" : 0 
     }, 
     "business_id" : "UB2j_EV3CIM_E4LcpadKMQ" 
    } 
] 
+0

有趣,因爲我從Yelp數據集中得到了這個。所以他們給我無效的JSON? – user2604504

+0

2個JSON哈希值是獨立有效的,所以也許你只是將​​它們連接在一起,而不是他們所期望的?或者,也許他們意味着獨立地有效,而不是在一起。如果你喜歡它,你可以一次解析它們(一行一行,如@ g.d.d.c建議)! –