1

I'm acctually having trouble with an json string an python. I've written a script that use find-my-iphone python module this module give me this string at the output

{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}

After a bit of prosesing with:

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

I obtain a string that look like this:

{"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}

There is my code: `

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("}","")

But when I try to use

json.loads(locstr)

Python give me :

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

I don't really know what I did wrong so I'am asking fo help

PS1: I just really need gps coordinate PS2: I'm french so sorry for mistake.

3 Answers3

1
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'}
pramod
  • 1,453
  • 1
  • 14
  • 16
0

Hi use the module ast,

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

Stack
  • 4,116
  • 3
  • 18
  • 23
0

Json does not support False and the postfix L in 1497518436368L; To make it regular, we need to convert it again

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

And now, we could use json.loads(s)

Jacky1205
  • 3,273
  • 3
  • 22
  • 44