调用Get-Connector获取30天数据时JSON解码失败问题
问题:获取30天数据时触发JSONDecodeError错误
我用Python代码通过Get-Connector获取数据,查询15天数据时运行正常,但查询30天数据时触发以下错误:
Traceback (most recent call last): File "C:\source\repos\Cabman Get-All\Cabman Get-All\Cabman_Get_All.py", line 46, in <module> jsondata = json.loads(data.content) File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2800.0_x64__qbz5n2kfrap0\lib\json\__init__.py", line 346, in loads return _default_decoder.decode(s) File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2800.0_x64__qbz5n2kfra80\lib\json\decoder.py", line 337, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2800.0_x64__qbz5n2kfra8p\lib\json\decoder.py", line 355, in raw_decode raise JSONDecodeError("Expecting value", s, err.value) from None json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
相关代码
用于获取数据并写入JSON文件的代码如下:
data = requests.get(url_data, headers = {'Host': 'services.%%%.com', 'Authorization': 'earer ' + acces_token}) status = data.status_code jsondata = json.loads(data.content) if status == 200: desired_dir = "B:\Cabman" full_path = os.path.join(desired_dir, x+'.json') with open(full_path, 'w') as f: json_string=json.dumps(jsondata, indent=4) f.write(json_string) print(x + " succesvol opgehaald.") else: print("Error at "+ x) print(status)
原始数据样例(敏感数据已掩码)
从Get-Connector获取的原始响应数据样例:
b'[{"shiftNumber":null,"egateShiftID":%%%,"weeknumber":6,"day":"zaterdag","hostId":7231,"driverCode":%%%,"driverName":"%%%","vehicleId":2751,"vehicleNr":"2270","licensePlate":"%%%","ShiftStart":"2023-02-11T07:58:33","FirstMovementTimeStamp":"2023-02-11T07:59:06","FirstTripTimeStamp":null,"diffLogonAndFirstMovement":0,"shiftStartLocation":{"latitude":0,"longitude":0},"LastMovementBeforeLogoffTimeStamp":"2023-02-11T08:00:36","ShiftEnd":"2023-02-11T08:01:47","diffLogoffAndLastMovement":1,"shiftEndLocation":{"latitude":%%%,"longitude":%%%},
生成的JSON文件样例(敏感数据已掩码)
小数据量下成功生成的JSON文件内容样例:
[ { "shiftNumber": null, "egateShiftID": %%%, "weeknumber": 4, "day": "zondag", "hostId": 7231, "driverCode": %%%, "driverName": "%%%", "vehicleId": 2380, "vehicleNr": "1866", "licensePlate": "%%%", "ShiftStart": "2023-01-29T06:34:44", "FirstMovementTimeStamp": null, "FirstTripTimeStamp": null, "diffLogonAndFirstMovement": null, "shiftStartLocation": { "latitude": %%%, "longitude": %%% }, "LastMovementBeforeLogoffTimeStamp": "2023-01-28T23:13:24", "ShiftEnd": "2023-01-29T06:59:14", "diffLogoffAndLastMovement": 465, "shiftEndLocation": {
问题集中在jsondata = json.loads(data.content)这一行,小数据量查询时代码运行正常,且requests.get没有返回错误状态码。
内容的提问来源于stack exchange,提问作者Catscanner
相关产品推荐
相关产品推荐

