【问题标题】:json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0) when using discord.pyjson.decoder.JSONDecodeError:期望值:使用 discord.py 时的第 1 行第 1 列(字符 0)
【发布时间】:2020-04-07 01:45:33
【问题描述】:

我正在编写机器人的一个非常常见的部分,它可以为不同的服务器有多个前缀,默认为“|”。

当我运行机器人并将其邀请到服务器时,这就是我加入时得到的结果:

Ignoring exception in on_guild_join
    Traceback (most recent call last):
      File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\site-packages\discord\client.py", line 270, in _run_event
        await coro(*args, **kwargs)
      File "C:\Users\amit2\source\repos\LAKRIS bot\LAKRIS bot\LAKRIS_bot.py", line 19, in on_guild_join
        prefixes = json.load(f)
      File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\json\__init__.py", line 296, in load
        parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
      File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\json\__init__.py", line 348, in loads
        return _default_decoder.decode(s)
      File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\json\decoder.py", line 337, in decode
        obj, end = self.raw_decode(s, idx=_w(s, 0).end())
      File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\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)

我的代码是:

 def get_prefix(bot, message):
    with open('prefixes.json', 'r') as f:
        prefixes = json.load(f)

    return prefixes[str(message.guild.id)]

bot = discord.Client()
bot = commands.Bot(command_prefix = get_prefix)

@bot.event
async def on_guild_join(guild):
    with open('prefixes.json', 'r') as f:
        prefixes = json.load(f)

    prefixes[str(guild.id)] = '|'

    with open('prefixes.json', 'w') as f:
        json.dump(prefixes, f, indent=4)

我尝试移动一些东西,但这是我第一次真正使用 json,所以我不确定要尝试什么。

编辑:这是我的项目文件。enter image description here

【问题讨论】:

  • 那个 json 文件里到底有什么?请记住,空文件不是有效的 json。

标签: python json discord.py


【解决方案1】:

我认为这意味着您的“prefixes.json”文件中存在语法错误。

它不包含有效的 json - 根据异常,第一个字符无效。

图像显示文件为 0 字节 - 空文件不是有效的 json - 您需要将一些 json 放入文件中。

【讨论】:

  • 我将项目文件添加到问题中,你能看到我错过的东西吗?谢谢
  • 是的,这就是问题所在。忘记了 {}。
猜你喜欢
  • 2023-03-25
  • 2020-11-11
  • 2017-05-09
  • 2019-08-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-21
相关资源
最近更新 更多