VS Code中的json库有问题吗?或者json响应无效?

0h4hbjxa  于 2023-03-31  发布在  其他
关注(0)|答案(2)|浏览(136)

我正在尝试从API调用响应。我不确定出了什么问题,也许响应不是有效的JSON,但我不确定。看起来json模块也没有初始化。我已经设法调用了没有json的数据。loads(data),但我需要将响应转换为dict,这样我就可以在格式化的字符串中使用它,例如print(“以下是{query}的三个建议的FAST术语:“,{auth1},{fast_id1},{auth2},{fast_id2},{auth3},{fast_id3})
验证码:

### assignFAST
# https://www.oclc.org/developer/api/oclc-apis/fast-api/assign-fast.en.html
# http://fast.oclc.org/searchfast/fastsuggest?&query=[query]&queryIndex=[queryIndex]&queryReturn=[queryReturn]&suggest=autosuggest&rows=[numRows]&callback=[callbackFunction]

import json
import requests

### CALLING API
data = requests.get('http://fast.oclc.org/searchfast/fastsuggest?&query=hog&queryIndex=suggestall&queryReturn=suggestall%2Cidroot%2Cauth%2Ctag%2Ctype%2Craw%2Cbreaker%2Cindicator&suggest=autoSubject&rows=3&callback=testcall').text

data_text = json.loads(data)

print(data_text)

错误:

Traceback (most recent call last):
  File "c:\Users\0001255336\OneDrive\Documents\Coding\assignFAST\assignFAST_V1.py", line 10, in <module>
    data_text = json.loads(data)
                ^^^^^^^^^^^^^^^^
  File "C:\Users\0001255336\AppData\Local\Programs\Python\Python311\Lib\json\__init__.py", line 346, in loads 
    return _default_decoder.decode(s)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\0001255336\AppData\Local\Programs\Python\Python311\Lib\json\decoder.py", line 337, in decode 
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\0001255336\AppData\Local\Programs\Python\Python311\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)
btxsgosb

btxsgosb1#

响应中有前缀

data[:10]
'testcall({'

如果你把它切下来就行了

data_text = json.loads(data[9:-1])

然而,似乎该网站不工作的财产,因为它不提供正确的JSON

polhcujo

polhcujo2#

因为你的json不正确。你的变量数据是字符串类型,因此不能作为json加载

相关问题