首页 > 解决方案 > JSON 格式疑难解答

问题描述

我正在尝试构建一个大型 JSON 文件,该文件是龙与地下城的纲要。我以为我的 JSON 格式正确,但是当我尝试将它提供给读取/导航 JSON 的程序时,它说它不是有效的 JSON 文件。有没有人能看一下,也许能指出我正确的方向?这是较大文件的缩短版本:

{"Items":{"Arrow of Slaying":{"Name":"Arrow of Slaying"},{"Detail":"very rare"},{"Magic":"Yes"},{"Roll":"6d10"},{"Description":"An arrow of slaying is a magic weapon meant to slay a particular kind of creature."},{"Type":"Arrow"},{"Weight":"0.05"}},{"Arrows":{"Name":"Arrows"},{"Description":"Ammunition: You can use a weapon that has the ammunition property to make a ranged attack only if you have ammunition to fire from the weapon. Each time you attack with the weapon, you expend one piece of ammunition. Drawing the ammunition from a quiver, case, or other container is part of the attack. At the end of the battle, you can recover half your expended ammunition by taking a minute to search the battlefield. Source: Player's Handbook p. 150"},{"Type":"Arrow"},{"Value":"0.05"},{"Weight":"0.05"}},{"Arrows +1":{"Name":"Arrows +1"},{"Detail":"uncommon"},{"Magic":"Yes"},{"Description":"You have a +1 bonus to attack and damage rolls made with this piece of magic ammunition. Once it hits a target, the ammunition is no longer magical. Source: Dungeon Master's Guide p. 150"},{"Type":"Arrow"},{"Weight":"0.05"}},{"Arrows +2":{"Name":"Arrows +2"},{"Detail":"rare"},{"Magic":"Yes"},{"Description":"You have a +2 bonus to attack and damage rolls made with this piece of magic ammunition. Once it hits a target, the ammunition is no longer magical. Source: Dungeon Master's Guide p. 150"},{"Type":"Arrow"},{"Weight":"0.05"}},{"Arrows +3":{"Name":"Arrows +3"},{"Detail":"very rare"},{"Magic":"Yes"},{"Description":"You have a +3 bonus to attack and damage rolls made with this piece of magic ammunition. Once it hits a target, the ammunition is no longer magical. Source: Dungeon Master's Guide p. 150"},{"Type":"Arrow"},{"Weight":"0.05"}},{"Blowgun Needle of Slaying":{"Name":"Blowgun Needle of Slaying"},{"Detail":"very rare"},{"Magic":"Yes"},{"Roll":"6d10"},{"Description":"A blowgun needle of slaying is a magic weapon meant to slay a particular kind of creature. Some are more focused than others; for example, there are both needles of dragon slaying and needles of blue dragon slaying. If a creature belonging to the type, race, or group associated with a needle of slaying takes damage from the needle the creature must make a DC 17 Constitution saving throw, taking an extra 6d10 piercing damage on a failed save, or half as much extra damage on a successful one. Once a needle of slaying deals its extra damage to a creature, it becomes a nonmagical blowgun needle. Source: Dungeon Master's Guide p. 152"},{"Type":"Arrow"},{"Weight":"0.02"}},{"Blowgun Needles":{"Name":"Blowgun Needles"},{"Description":"Ammunition: You can use a weapon that has the ammunition property to make a ranged attack only if you have ammunition to fire from the weapon. Each time you attack with the weapon, you expend one piece of ammunition. Drawing the ammunition from a quiver, case, or other container is part of the attack. At the end of the battle, you can recover half your expended ammunition by taking a minute to search the battlefield. Source: Player's Handbook p. 150"},{"Type":"Arrow"},{"Value":"0.02"},{"Weight":"0.02"}},{"Blowgun Needles +1":{"Name":"Blowgun Needles +1"},{"Detail":"uncommon"},{"Magic":"Yes"},{"Description":"You have a +1 bonus to attack and damage rolls made with this piece of magic ammunition. Once it hits a target, the ammunition is no longer magical. Source: Dungeon Master's Guide p. 150"},{"Type":"Arrow"},{"Weight":"0.02"}},{"Blowgun Needles +2":{"Name":"Blowgun Needles +2"},{"Detail":"rare"},{"Magic":"Yes"},{"Description":"You have a +2 bonus to attack and damage rolls made with this piece of magic ammunition. Once it hits a target, the ammunition is no longer magical. Source: Dungeon Master's Guide p. 150"},{"Type":"Arrow"},{"Weight":"0.02"}},{"Blowgun Needles +3":{"Name":"Blowgun Needles +3"},{"Detail":"very rare"},{"Magic":"Yes"},{"Description":"You have a +3 bonus to attack and damage rolls made with this piece of magic ammunition. Once it hits a target, the ammunition is no longer magical. Source: Dungeon Master's Guide p. 150"},{"Type":"Arrow"},{"Weight":"0.02"}},{"Crossbow Bolt of Slaying":{"Name":"Crossbow Bolt of Slaying"},{"Detail":"very rare"},{"Magic":"Yes"},{"Roll":"6d10"},{"Description":"A crossbow bolt of slaying is a magic weapon meant to slay a particular kind of creature. Some are more focused than others; for example, there are both bolts of dragon slaying and bolts of blue dragon slaying. If a creature belonging to the type, race, or group associated with a bolt of slaying takes damage from the bolt the creature must make a DC 17 Constitution saving throw, taking an extra 6d10 piercing damage on a failed save, or half as much extra damage on a successful one. Once a bolt of slaying deals its extra damage to a creature, it becomes a nonmagical crossbow bolt. Source: Dungeon Master's Guide p. 152"},{"Type":"Arrow"},{"Weight":"0.075"}},{"Crossbow Bolts":{"Name":"Crossbow Bolts"},{"Description":"Ammunition: You can use a weapon that has the ammunition property to make a ranged attack only if you have ammunition to fire from the weapon. Each time you attack with the weapon, you expend one piece of ammunition. Drawing the ammunition from a quiver, case, or other container is part of the attack. At the end of the battle, you can recover half your expended ammunition by taking a minute to search the battlefield. Source: Player's Handbook p. 150"},{"Type":"Arrow"},{"Value":"0.05"},{"Weight":"0.075"}},{"Crossbow Bolts +1":{"Name":"Crossbow Bolts +1"},{"Detail":"uncommon"},{"Magic":"Yes"},{"Description":"You have a +1 bonus to attack and damage rolls made with this piece of magic ammunition. Once it hits a target, the ammunition is no longer magical. Source: Dungeon Master's Guide p. 150"},{"Type":"Arrow"},{"Weight":"0.075"}},{"Crossbow Bolts +2":{"Name":"Crossbow Bolts +2"},{"Detail":"rare"},{"Magic":"Yes"},{"Description":"You have a +2 bonus to attack and damage rolls made with this piece of magic ammunition. Once it hits a target, the ammunition is no longer magical. Source: Dungeon Master's Guide p. 150"},{"Type":"Arrow"},{"Weight":"0.075"}},{"Crossbow Bolts +3":{"Name":"Crossbow Bolts +3"},{"Detail":"very rare"},{"Magic":"Yes"},{"Description":"You have a +3 bonus to attack and damage rolls made with this piece of magic ammunition. Once it hits a target, the ammunition is no longer magical. Source: Dungeon Master's Guide p. 150"},{"Type":"Arrow"},{"Weight":"0.075"}},{"Wand of Wonder":{"Name":"Wand of Wonder"},{"Detail":"rare (requires attunement by a Spellcaster)"},{"Magic":"Yes"},{"Roll":"1d6+1"},{"Description":"This wand has 7 charges."},{"Type":"Miscellaneous"},{"Weight":"1"}}}

标签: json

解决方案


看起来您的 json 对象存在嵌套问题。

看看你的 json 的第一部分:

{
    "Items": {
        "Arrow of Slaying": {
            "Name": "Arrow of Slaying"
        },
        {
            "Detail": "very rare"
        },
        {
            "Magic": "Yes"
        },
        {
            "Roll": "6d10"
        },
        {
            "Description": "An arrow of ..."
        },
        {
            "Type": "Arrow"
        },
        {
            "Weight": "0.05"
        }
    },
    {
        "Arrows": {
            "Name": "Arrows"
        },
        {
            "Description": "Ammunition: You can use a ..."
        },
        {
            "Type": "Arrow"
        },
        {
            "Value": "0.05"
        },
        {
            "Weight": "0.05"
        }
    },
    {
...

看起来您正在尝试将项目的详细信息分组到其父对象中,但实际上您将每个事物分组为自己的对象,从而导致嵌套问题。

这是一个清理后的版本:

{
    "Items": {
        "Arrow of Slaying": {
            "Name": "Arrow of Slaying",
            "Detail": "very rare",
            "Magic": "Yes",
            "Roll": "6d10",
            "Description": "An arrow of slaying is a magic weapon meant to slay a particular kind of creature.",
            "Type": "Arrow",
            "Weight": "0.05"
        },
        "Arrows": {
            "Name": "Arrows",
            "Description": "Ammunition: You can use a weapon that has the ammunition property to make a ranged attack only if you have ammunition to fire from the weapon. Each time you attack with the weapon, you expend one piece of ammunition. Drawing the ammunition from a quiver, case, or other container is part of the attack. At the end of the battle, you can recover half your expended ammunition by taking a minute to search the battlefield. Source: Player's Handbook p. 150",
            "Type": "Arrow",
            "Value": "0.05",
            "Weight": "0.05",
        },
        "Continue items here": {
            "Name": "Name",
            "Description": "..."
        }
    }
}

您可以立即知道每个项目及其属性都得到了适当的组织。因此,如果您需要使用typeofArrow of Slaying您可以通过以下方式访问它:jsonVariableName['Items']['Arrow of Slaying']['Type'];

根据您正在开发的IDE,您可能需要添加一个json beautify addon/ extension。我发现如果出现问题,使用像codebeautify这样的浏览器工具只会返回错误。相反,如果您使用atom-beautify包,它会扩展它并让您知道错误在哪里。这样你至少有一些东西,而不是一个对你没有帮助的一般错误。

希望这可以帮助 :)


推荐阅读