Python 从API获取响应时发生JsonDecodeError

Python 从API获取响应时发生JsonDecodeError,python,json,api,Python,Json,Api,因此,我尝试从api获取一个简单响应,当我尝试从api获取响应时,我得到一个错误: JSONDecodeError:应为值:第1行第1列(字符0) 回溯如下: --------------------------------------------------------------------------- JSONDecodeError Traceback (most recent call last) <ipython-input-

因此,我尝试从api获取一个简单响应,当我尝试从api获取响应时,我得到一个错误: JSONDecodeError:应为值:第1行第1列(字符0)

回溯如下:

---------------------------------------------------------------------------
JSONDecodeError                           Traceback (most recent call last)
<ipython-input-27-c5319b3765a1> in <module>
----> 1 gg = json.dumps(response.json(), indent = 4)

~\anaconda3\lib\site-packages\requests\models.py in json(self, **kwargs)
    895                     # used.
    896                     pass
--> 897         return complexjson.loads(self.text, **kwargs)
    898 
    899     @property

~\anaconda3\lib\json\__init__.py in loads(s, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
    346             parse_int is None and parse_float is None and
    347             parse_constant is None and object_pairs_hook is None and not kw):
--> 348         return _default_decoder.decode(s)
    349     if cls is None:
    350         cls = JSONDecoder

~\anaconda3\lib\json\decoder.py in decode(self, s, _w)
    335 
    336         """
--> 337         obj, end = self.raw_decode(s, idx=_w(s, 0).end())
    338         end = _w(s, end).end()
    339         if end != len(s):

~\anaconda3\lib\json\decoder.py in raw_decode(self, s, idx)
    353             obj, end = self.scan_once(s, idx)
    354         except StopIteration as err:
--> 355             raise JSONDecodeError("Expecting value", s, err.value) from None
    356         return obj, end

JSONDecodeError: Expecting value: line 1 column 1 (char 0)
​
---------------------------------------------------------------------------
JSONDecodeError回溯(最近一次调用)
在里面
---->1 gg=json.dumps(response.json(),indent=4)
json格式的~\anaconda3\lib\site packages\requests\models.py(self,**kwargs)
895已使用。
896通行证
-->897返回complexjson.load(self.text,**kwargs)
898
899@property
加载中的~\anaconda3\lib\json\\uuuuuu init\uuuuuuuuuuuuuuuuuuuuuuuuuuuuupy(s、编码、cls、对象钩子、解析浮点数、解析整型、解析常量、对象对钩子、**kw)
346 parse_int为无,parse_float为无且
347 parse_常量为None且对象_pairs_hook为None且非kw):
-->348返回默认解码器。解码
349如果cls为无:
350 cls=JSONDecoder
解码中的~\anaconda3\lib\json\decoder.py(self,s,\u w)
335
336         """
-->337 obj,end=self.raw\u decode(s,idx=\u w(s,0.end())
338 end=_w(s,end).end()
339如果结束!=长度:
原始解码中的~\anaconda3\lib\json\decoder.py(self、s、idx)
353 obj,end=自扫描一次(s,idx)
354除了停止迭代作为错误:
-->355将JSONDecodeError(“预期值”,s,err.value)从None提升
356返回obj,结束
JSONDecodeError:应为值:第1行第1列(字符0)
​
我在这里到底做错了什么?我还想知道如何使代码更好或更高效,我仍在学习如何编写python代码


谢谢!

您正在尝试使用API的HTML版本。请将代码指向下面的URL

import requests
import json
current = "https://covidtracking.com/api/v1/states/current.json"
response = requests.get(current)
print(response.status_code)
#get the data from the API response as JSON
covid_data = response.json()
#Since we have the data by state, loop through each state
for data in covid_data:
    #read the individual attributes from the data for each state and print / process as required.
    print(f"{data['state']}-{data['positive']}")
请参阅此页,其中提供了返回JSON的API和返回CSV的API的详细信息。

请转到此处:
import requests
import json
current = "https://covidtracking.com/api/v1/states/current.json"
response = requests.get(current)
print(response.status_code)
#get the data from the API response as JSON
covid_data = response.json()
#Since we have the data by state, loop through each state
for data in covid_data:
    #read the individual attributes from the data for each state and print / process as required.
    print(f"{data['state']}-{data['positive']}")