【问题标题】:"Expecting property name enclosed in double quotes" in DiscordDiscord中的“期望用双引号括起来的属性名称”
【发布时间】:2019-09-11 22:06:32
【问题描述】:

我正在编写一个不和谐的机器人来和我的朋友一起使用,我在我的一个齿轮上收到一条奇怪的消息。该代码在一个 cog 上完美运行,而在另一个 cog 上则完全不行。

我尝试重写命令并尝试以不同的方式获取数据无济于事。

import discord
import json
import os
from discord.ext import commands

class Inventory(commands.Cog):

    def __init__(self, client):
        self.client = client

    @commands.command(aliases=['iindex', 'ii'])
    async def itemindex(self, ctx, arg1='nd', arg2='nd'):
        await ctx.channel.purge(limit=1)
        if arg1 == 'nd':
            embed=discord.Embed(title="insert title", description='insert text', color=0x02a5fd)
            embed.set_footer(text="insert footer")
            await ctx.send(embed=embed)

        if arg1 == 'standard':

            if arg2 == 'nd':
                _n = 1
                _a = 0
                embed=discord.Embed(title="insert title", color=0x02a5fd)
                with open('database.json', 'r') as f:
                    data = json.load(f) #error on this line

                while _a < 20:
                    for item in data['items']['standard']:
                        if data['items']['standard'][item]['itemid'] == _n:
                            name = data['items']['standard'][item]['name']
                            embed.add_field(name=f'{name}', value=f'ID: {_n}', inline=False)
                            _n += 1
                            _a += 1

                embed.set_footer(text="insert footer")
                await ctx.send(embed=embed)

def setup(client):
    client.add_cog(Inventory(client))

它应该做的是加载文件并循环遍历密钥中的项目,但它甚至不会到达那一点。相反,它给了我这个错误:

Ignoring exception in command itemindex:
Traceback (most recent call last):
  File "C:\Users\stars\AppData\Local\Programs\Python\Python36\lib\site-packages\discord\ext\commands\core.py", line 79, in wrapped
    ret = await coro(*args, **kwargs)
  File "C:\Users\stars\Documents\Projects\Discord\Neximator\cogs\inventory.py", line 30, in itemindex
    data = json.load(f)
  File "C:\Users\stars\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\stars\AppData\Local\Programs\Python\Python36\lib\json\__init__.py", line 354, in loads
    return _default_decoder.decode(s)
  File "C:\Users\stars\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\stars\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 property name enclosed in double quotes: line 43 column 5 (char 1218)

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

Traceback (most recent call last):
  File "C:\Users\stars\AppData\Local\Programs\Python\Python36\lib\site-packages\discord\ext\commands\bot.py", line 859, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Users\stars\AppData\Local\Programs\Python\Python36\lib\site-packages\discord\ext\commands\core.py", line 725, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "C:\Users\stars\AppData\Local\Programs\Python\Python36\lib\site-packages\discord\ext\commands\core.py", line 88, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: JSONDecodeError: Expecting property name enclosed in double quotes: line 43 column 5 (char 1218)

是我做错了什么还是这是一个错误?

编辑:我现在意识到这可能是一个 json 错误,因为我对 JSON 很陌生,并不真正了解所有规则。这是我正在使用的 database.json 文件:

{
  "nicknames": {
    "discordname": "nickname",
  },
  "items": {
    "standard": {
      "slimeball": {
        "name": "Slimeball",
        "description": "Ball of sticky, viscous slime dropped by... well... slimes.",
        "rarity": "D",
        "itemid": 1
      }
    },
    "weapon": {
      "wooden_dagger": {
        "name": "Wooden Dagger",
        "description": "A stick with an edge lazily carved into it.",
        "type": "sword",
        "damage": "1d3",
        "accuracy": "1d20",
        "weaponid": 1
      },
      "wooden_gaunlets": {
        "name": "Wooden Gaunlets",
        "description": "Two logs with holes for your hands.  There are several designs lazily cut into it.",
        "type": "melee",
        "damage": "2d2",
        "accuracy": "2d10",
        "weaponid": 2
      },
      "wooden_bow": {
        "name": "Wooden Bow",
        "description": "A stick with a thin strand of elastic threaded through the ends creating an arch.",
        "type": "ranged",
        "damage": "1d4",
        "accuracy": "1d25",
        "weaponid": 3
      },
    }
  },
  "enemies": {
  }
}

【问题讨论】:

  • 您的database.json 文件是否包含无效的 JSON,例如用单引号括起来的字符串?
  • 未来,请注意,问题标题应侧重于帮助其他人弄清楚他们是否有同样的问题,其次,帮助人们找出问题所在通常是要确定他们是否能够有效地帮助回答它。其他人“在运行可在其他地方运行的代码时出现奇怪错误”的人不太可能出现 same“奇怪错误”,因此该问题的初始标题对这两个目标都没有帮助。
  • 我已编辑以包含 json 文件。我是那种语言的新手,所以我自己不太确定规则。我浏览了一下,找不到任何单引号,但也许是别的东西?

标签: python json python-3.x discord.py


【解决方案1】:

这是因为 f 不是有效的 JSON。

它为您提供了一个非常明确的错误,说明原因。 Expecting property name enclosed in double quotes

没有看到你的 json 文件,我真的不能说更多。但我假设 database.json 不是有效的 JSON。我的猜测是您在键上使用了单引号。

【讨论】:

  • 我已编辑以包含 json 文件。我是那种语言的新手,所以我自己不太确定规则。我浏览了一下,找不到任何单引号,但也许是别的东西?
  • 没关系,我找到了问题所在。我的 json 文件的第 42 行有一个额外的逗号。里面还是有错误,但是和json文件没有关系了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-22
  • 1970-01-01
  • 2019-01-04
  • 2019-04-24
  • 1970-01-01
相关资源
最近更新 更多