如何调试Python JSONdecode错误?

ljsrvy3e  于 2023-07-01  发布在  Python
关注(0)|答案(3)|浏览(140)

很抱歉这个奇怪的问题,我不能以其他方式提交。我正在尝试创建聊天机器人命令。但是我总是得到愚蠢的JSON错误。
下面是我的代码:

@client.command()
async def chatbot(ctx, *, msg):
    chatbot1 = requests.get(f"https://chatbot-api.therealenny1.repl.co/?message={quote(msg)}")
    resp = chatbot1.json()
    await ctx.send(resp)

下面是错误:

Ignoring exception in command chatbott:
Traceback (most recent call last):
  File "/home/runner/m/venv/lib/python3.8/site-packages/requests/models.py", line 972, in json
    return complexjson.loads(self.text, **kwargs)
  File "/nix/store/p21fdyxqb3yqflpim7g8s1mymgpnqiv7-python3-3.8.12/lib/python3.8/json/__init__.py", line 357, in loads
    return _default_decoder.decode(s)
  File "/nix/store/p21fdyxqb3yqflpim7g8s1mymgpnqiv7-python3-3.8.12/lib/python3.8/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/nix/store/p21fdyxqb3yqflpim7g8s1mymgpnqiv7-python3-3.8.12/lib/python3.8/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)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/runner/m/venv/lib/python3.8/site-packages/discord/ext/commands/core.py", line 85, in wrapped
    ret = await coro(*args, **kwargs)
  File "main.py", line 50, in chatbott
    resp = chatbot1.json()
  File "/home/runner/m/venv/lib/python3.8/site-packages/requests/models.py", line 976, in json
    raise RequestsJSONDecodeError(e.msg, e.doc, e.pos)
requests.exceptions.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/runner/m/venv/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 939, in invoke
    await ctx.command.invoke(ctx)
  File "/home/runner/m/venv/lib/python3.8/site-packages/discord/ext/commands/core.py", line 863, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "/home/runner/m/venv/lib/python3.8/site-packages/discord/ext/commands/core.py", line 94, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: JSONDecodeError: Expecting value: line 1 column 1 (char 0)

修好了
修复:

@client.command()
async def chatbot(ctx, msg):
  url = f"https://chatbot-api.therealenny1.repl.co/?message={quote(msg)}"
  data = requests.get(url)
  await ctx.send(data.text)
vmjh9lq9

vmjh9lq91#

虽然识别json错误可能并不总是那么容易,但你得到的错误是在第一个字符上。Expecting value: line 1 column 1 (char 0)我的猜测是您根本没有收到jsone消息。开始,检查resp.status_code,看看是否得到200响应或其他结果。您还可以通过使用resp.text打印/记录响应的文本来进行调试,这可能会给您提供更多的线索。

lvjbypge

lvjbypge2#

找到解决办法了。

@client.command()
async def chatbot(ctx, msg):
  url = f"https://chatbot-api.therealenny1.repl.co/?message={quote(msg)}"
  data = requests.get(url)
  await ctx.send(data.text)
yxyvkwin

yxyvkwin3#

要回答标题问题“如何调试Python JSONdecode错误?“(来到这里问这个问题),我发现有很多在线JSON验证器,例如。https://jsonlint.com/
复制粘贴错误的JSON字符串,任何错误的位置都将突出显示。

相关问题