JSON无法导入

时间:2019-02-02 11:15:44

标签: json python-3.6 discord.py

我创建一个不和谐主持人机器人,我想创建一个“每个服务器的自定义前缀。”

我的问题是我正在尝试导入json库,但是它不起作用。返回错误。

我正在使用以下代码(测试代码)

from discord.ext import commands
import discord
import json
import asyncio

with open("prefixes.json") as json_data:
    prefixes = json.load(json_data)
default_prefix = "!"

def prefix(bot, message):
    id = message.server.id
    return prefixes.get(id, default_prefix)

bot = commands.Bot(command_prefix=prefix)

@bot.command(name="prefix", pass_context=True)
async def _prefix(ctx, new_prefix):
    # Do any validations you want to do
    prefixes[ctx.message.server.id] = new_prefix
    with open("prefixes.json", "w") as f:
        json.dump(prefixes, f)


. . .

我从上面的代码中收到此错误:

===== RESTART: C:\Users\Raphaël\Desktop\Python\Moderator Bot\coreTest.py =====
Traceback (most recent call last):
  File "C:\Users\Raphaël\Desktop\Python\Moderator Bot\coreTest.py", line 6, in <module>
    prefixes = json.load(json_data)
  File "C:\Users\Raphaël\AppData\Local\Programs\Python\Python36\lib\json\__init__.py", line 299, in load
    parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
  File "C:\Users\Raphaël\AppData\Local\Programs\Python\Python36\lib\json\__init__.py", line 354, in loads
    return _default_decoder.decode(s)
  File "C:\Users\Raphaël\AppData\Local\Programs\Python\Python36\lib\json\decoder.py", line 339, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Users\Raphaël\AppData\Local\Programs\Python\Python36\lib\json\decoder.py", line 355, in raw_decode
    obj, end = self.scan_once(s, idx)
json.decoder.JSONDecodeError: Expecting ',' delimiter: line 3 column 2 (char 30)
>>> 

1 个答案:

答案 0 :(得分:0)

我找到了问题的根源,我忘记了逗号。 现在可以了。