从api获取响应时如何更正JSONDECODERROR

问题描述 投票:0回答:1

我正在尝试从公共api获取响应,但遇到一个错误,指出“ json.decoder.JSONDecodeError:期望值:第1行第1列(字符0)”

我的代码如下:

from datetime import datetime
from datetime import timedelta
import json
import requests

##historical state data
start_date = input("Enter Start Date (YYYYDDMM)")
end_date = input("Enter End Date (YYYYDDMM)")

start_date = datetime.strptime(start_date, "%Y%m%d").date()
end_date = datetime.strptime(end_date, "%Y%m%d").date()
states = ['AL', 'AK', 'AZ', 'AR', 'CA', 'CO', 'CT', 'DE', 'FL', 'GA', 'HI', 'ID', 'IL', 'IN', 'IA', 'KS', 'KY', 'LA', 'ME', 'MD', 'MA', 'MI', 'MN', 'MS', 'MO', 'MT', 'NE', 'NV', 'NH', 'NJ', 'NM', 'NY', 'NC', 'ND', 'OH', 'OK', 'OR', 'PA', 'RI', 'SC', 'SD', 'TN', 'TX', 'UT', 'VT', 'VA', 'WA', 'WV', 'WI', 'WY']
length = len(states)
i = 0
date = start_date
while date <= end_date:
    while i < length:
        date_str = date.strftime("%Y%d%m")
        hist_state_data = requests.get("https://covidtracking.com/api/v1/states/" + states[i] + "/" + date_str + ".json")
        if hist_state_data.ok or type(hist_state_data.content) is not bytes:
              i+=1
              continue
        hist_state_data = json.loads(hist_state_data.content.decode("utf-8"))
        print(date_str)
        i+=1
        print({hist_state_data["deaths"]})
    date += timedelta(days=1)

并且回溯是:

Traceback (most recent call last):
  File "C:/Users/****/PycharmProjects/COVID_API/API_REQUESTS.py", line 48, in <module>
    hist_state_data = json.loads(hist_state_data.content.decode("utf-8"))
  File "C:\Users\****\anaconda3\envs\COVID_API\lib\json\__init__.py", line 357, in loads
    return _default_decoder.decode(s)
  File "C:\Users\****\anaconda3\envs\COVID_API\lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Users\****\anaconda3\envs\COVID_API\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)

[不幸的是,在检查整个代码是否符合预期的情况之前,我无法克服这个错误。请让我知道如何解决此问题,以及是否有我可以为将来的实例学习的文档!

edit依次添加了导入并将vi更改为v1

python json valueerror
1个回答
0
投票

您在API URL中输入错误。应该是v1,而不是vi

data = requests.get("https://covidtracking.com/api/v1/states/AL/20200519.json").json()
print(json.dumps(data, indent=4))
# {
#     "date": 20200519,
#     "state": "AL",
#     "positive": 12376,
#     "negative": 145190,
#     "pending": null,
#     "hospitalizedCurrently": null,
#     "hospitalizedCumulative": 1453,
#     "inIcuCurrently": null,
#     "inIcuCumulative": 517,
#     "onVentilatorCurrently": null,
#     "onVentilatorCumulative": 306,
#     "recovered": null,
#     "dataQualityGrade": "B",
#     "lastUpdateEt": "5/18/2020 00:00",
#     "hash": "da768efac6b967b10afef712b89563d433bfa593",
#     "dateChecked": "2020-05-19T20:00:00Z",
#     "death": 504,
#     "hospitalized": 1453,
#     "total": 157566,
#     "totalTestResults": 157566,
#     "posNeg": 157566,
#     "fips": "01",
#     "deathIncrease": 15,
#     "hospitalizedIncrease": 37,
#     "negativeIncrease": 153,
#     "positiveIncrease": 290,
#     "totalTestResultsIncrease": 443
# }
© www.soinside.com 2019 - 2024. All rights reserved.