2017-06-15 86 views
0

我正在麻煩json字符串python。我寫了一個腳本,使用find-MY-iphone Python模塊該模塊給我這個字符串在輸出Python json prase ValueError:沒有JSON對象可以解碼

{u'locationType': u'', u'altitude': 0.0, u'locationFinished': True, u'longitude': 7.340714223689717, u'positionType': u'GPS', u'floorLevel': 0, u'timeStamp': 1497518502892L, u'latitude': 47.81268700030429, u'isOld': False, u'isInaccurate': False, u'verticalAccuracy': 0.0, u'horizontalAccuracy': 50.0}

一點與prosesing後:

loc = api.devices[deviceID].location() 
locstr = str(loc).replace("u'",'"').replace("'",'"') #.replace("}","") 

我得到一個字符串,是這樣的:

{"locationType": "", "altitude": 0.0, "locationFinished": False, "longitude": 7.340450948111099, "positionType": "GPS", "floorLevel": 0, "timeStamp": 1497518436368L, "latitude": 47.81275740829093, "isOld": False, "isInaccurate": False, "verticalAccuracy": 0.0, "horizontalAccuracy": 100.0}

有我的代碼: `

from pyicloud import PyiCloudService 
from geopy.distance import vincenty 
import json 
import sys 

api = PyiCloudService('*****.*****@free.fr','******') 
deviceID = u"Qo+Jyvct3IIl7N3MXrz6LfDvm8qjDCHjkedOvse1mhzWf1sikvSFQOHYVNSUzmWV"   # Needed 
deviceNAME = "<AppleDevice(iPhone 5s: David Smartphone)>"        # Just an help 

api.devices[deviceID].location() 
api.devices[deviceID].status() 

loc = api.devices[deviceID].location() 
locstr = str(loc).replace("u'",'"').replace("'",'"') #.replace("}","") 

但是當我嘗試使用

json.loads(locstr)

的Python給我:

Traceback (most recent call last): File "distancePAPA.py", line 19, in t = json.loads(locstr) File "/usr/lib/python2.7/json/init.py", line 339, in loads return _default_decoder.decode(s) File "/usr/lib/python2.7/json/decoder.py", line 364, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "/usr/lib/python2.7/json/decoder.py", line 382, in raw_decode raise ValueError("No JSON object could be decoded") ValueError: No JSON object could be decoded

我真的不知道我做錯了什麼,以便我'問FO幫助

PS1:我真的需要GPS座標 PS2:我是法國人,非常抱歉有錯。

回答

0
import json 

>>> loc = api.devices[deviceID].location() 
>>> locstr = json.dumps(loc) 
'{"horizontalAccuracy": 50.0, "floorLevel": 0, "isOld": false, "isInaccurate": false, "verticalAccuracy": 0.0, "timeStamp": 1497518502892, "altitude": 0.0, "locationFinished": true, "longitude": 7.340714223689717, "positionType": "GPS", "locationType": "", "latitude": 47.81268700030429}' 

>>> json.loads(locstr) 
{u'timeStamp': 1497518502892, u'altitude': 0.0, u'locationFinished': True, u'longitude': 7.340714223689717, u'horizontalAccuracy': 50.0, u'floorLevel': 0, u'locationType': u'', u'latitude': 47.81268700030429, u'isOld': False, u'isInaccurate': False, u'verticalAccuracy': 0.0, u'positionType': u'GPS'} 
+0

感謝您的解決方案,我finnaly使用您的 – TurtleForGaming

0

喜用模塊AST

import ast loc_str = ast.literal_eval(str(loc))

0

的Json不支持False1497518436368L後綴L;爲了使常規,我們需要再次轉換

s = locstr.replace('False', 'false').replace('L,', ',') 

而現在,我們可以使用json.loads(s)

相關問題