2017-05-09 91 views
-2

穿幫哀悼蟒蛇加載JSON文件,使用導致錯誤

當我試圖JSON文件加載到MongoDB中,我收到以下錯誤:

raise ValueError("No JSON object could be decoded"). 

在我看來,我的問題來自我的第二個領域,但我不知道如何將名稱更改爲名稱,或者在刪除之前加載它。

我的JSON文件是:

{ 
"_id" : "585a9ecec62747d1e19497a5", 
"" : NumberInt(0), 
"VendorID" : NumberInt(2), 
"lpep_pickup_datetime" : "2015-11-01 00:57:34", 
"Lpep_dropoff_datetime" : "2015-11-01 23:57:45", 
"Store_and_fwd_flag" : "N", 
"RateCodeID" : NumberInt(5), 
"Pickup_longitude" : -73.9550857544, 
"Pickup_latitude" : 40.6637229919, 
"Dropoff_longitude" : -73.958984375, 
"Dropoff_latitude" : 40.6634483337, 
"Passenger_count" : NumberInt(1), 
"Trip_distance" : 0.09, 
"Fare_amount" : 15.0, 
"Extra" : 0.0, 
"MTA_tax" : 0.0, 
"Tip_amount" : 0.0, 
"Tolls_amount" : 0.0, 
"Ehail_fee" : "", 
"improvement_surcharge" : 0.0, 
"Total_amount" : 15.0, 
"Payment_type" : NumberInt(2), 
"Trip_type" : NumberInt(2), 
"x" : -8232642.48775, 
"y" : 4962866.701, 
"valid_longitude" : NumberInt(1), 
"valid_latitude" : NumberInt(1), 
"valid_coordinates" : NumberInt(2) 
} 
+0

當你使用Python時,請用它來標記你的問題。 – trincot

+0

請單擊[編輯](http://stackoverflow.com/posts/43866216/edit)鏈接並更新您的問題,以顯示試圖讀取此數據的代碼。 –

回答

0

在你的JSON文件的問題是不是空字符串鍵(即是允許的),但NumberInt(...)的出現:這是不是有效的JSON。您需要提供該號碼,而不用將其包裝在某種功能中。

因此,這將是有效的:

{ 
    "_id": "585a9ecec62747d1e19497a5", 
    "": 0, 
    "VendorID": 2, 
    "lpep_pickup_datetime": "2015-11-01 00:57:34", 
    "Lpep_dropoff_datetime": "2015-11-01 23:57:45", 
    "Store_and_fwd_flag": "N", 
    "RateCodeID": 5, 
    "Pickup_longitude": -73.9550857544, 
    "Pickup_latitude": 40.6637229919, 
    "Dropoff_longitude": -73.958984375, 
    "Dropoff_latitude": 40.6634483337, 
    "Passenger_count": 1, 
    "Trip_distance": 0.09, 
    "Fare_amount": 15.0, 
    "Extra": 0.0, 
    "MTA_tax": 0.0, 
    "Tip_amount": 0.0, 
    "Tolls_amount": 0.0, 
    "Ehail_fee": "", 
    "improvement_surcharge": 0.0, 
    "Total_amount": 15.0, 
    "Payment_type": 2, 
    "Trip_type": 2, 
    "x": -8232642.48775, 
    "y": 4962866.701, 
    "valid_longitude": 1, 
    "valid_latitude": 1, 
    "valid_coordinates": 2 
} 

如果你有過非JSON文件沒有控制權,然後讀取文件內容後替換NumberInt的出現像這樣(在Python):

import re 
json = re.sub(r"NumberInt\((\d+)\)", r"\1", json) 
+0

import re json = re.sub(r「NumberInt \((\ d +)\)」,r「\ 1」,data_link) print json am I I correct? – smb

+0

我應該用open打開('/ home/sebastien/SF2/test/test.json','r')來替換json。我仍然收到錯誤:TypeError:預期的字符串或緩衝區。 – smb

+0

這只是讀取變量中的文件的問題。例如:'打開('myfile','r')爲f:json = f.read()',然後繼續使用我提供的代碼。 – trincot