gpt4 book ai didi

Python json 编写 ValueError : No JSON object could be decoded

转载 作者:太空宇宙 更新时间:2023-11-03 15:07:05 27 4
gpt4 key购买 nike

我实际上在使用 python 的 json 字符串时遇到了麻烦。我编写了一个使用 find-my-iphone python 模块的脚本,该模块在输出处给我这个字符串

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

经过一番写作:

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

我真的不知道我做错了什么,所以我请求帮助

PS1:我真的需要 GPS 坐标PS2:我是法国人,很抱歉犯了错误。

最佳答案

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

关于Python json 编写 ValueError : No JSON object could be decoded,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44564017/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com