我正在尝试构建一个大型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"}}}
答案 0 :(得分:0)
要验证JSON,您可以使用一些在线验证器(例如https://jsonlint.com)。
对于您的情况,似乎您想创建对象数组,因此看起来应该像这样(只是json的一部分):
{
"Items": [
{
"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"
},
{
"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"
},
{
"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"
}
]
}
答案 1 :(得分:0)
似乎您的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": "..."
}
}
}
您可以立即告诉您每个项目及其属性都已正确组织。因此,如果您需要使用type
中的Arrow of Slaying
,则可以通过以下方式访问它:jsonVariableName['Items']['Arrow of Slaying']['Type'];
根据开发的IDE,可能要添加一个json beautify addon/ extension。我发现使用codebeautify之类的浏览器工具只会在出现问题时返回错误。相反,如果您使用atom-beautify软件包,它将对其进行扩展并告知错误在哪里。这样一来,您至少会遇到一些问题,而不是对您没有帮助的一般错误。
希望这会有所帮助:)
答案 2 :(得分:0)
程序正确,不是有效的JSON。每个对象(即{...}
)的属性必须带有名称(用引号引起来),冒号和值(可以是另一个对象)。看一下您的JSON的简单版本(...
代表了其余内容):
{
"Items": {
"Arrow of Slaying": {
"Name": "Arrow of Slaying"
},
{ "Detail": "very rare" },
...
}
}
我认为"Detail"
应该是"Arrow of Slaying"
之后相同对象的属性,而不是新对象。我认为正确的JSON如下:
{
"Items": {
"Arrow of Slaying": {
"Name": "Arrow of Slaying",
"Detail": "very rare",
...
},
...
}
}