0

我正在尝试在 Groovy 中解析一个 Json 文件,其中包含一个Listof Strings。其中一些字符串包含GString格式,因此我希望将它们解析为GStrings 而不是普通String的 s。

例如,description下面的成员将包含 1GString和 7 Strings:

{
  ... (other members)
  "description": [
    "This weapon features an alternate firing mode which shoots a tracker grenade.",
    "As a free action, you can switch to the alternate fire mode.",
    "Grenades can be fired up to 30 feet and detonate after 1 round.",
    "Creatures within 15 feet of the grenade must make a Dexterity saving throw.",
    "The save DC is equal to 8 + your Wisdom modifier + your proficiency bonus.",
    "On a failed save, a creature takes ${variables}.get('grenadeDamage').get(${level}) damage and is considered tagged for 2 rounds.",
    "On a success, a creature takes only half the damage and is not tagged.",
    "Attacks with this weapon against tagged creatures have advantage.",
    "However, if any creatures are tagged, this weapon can only attack those creatures."
  ],
  "grenadeDamage": [
    "1d4", "1d4", "1d4", "1d6", "1d6",
    "1d6", "1d8", "1d8", "1d8", "1d10",
    "1d10", "1d10", "2d6", "2d6", "2d6",
    "4d4", "4d4", "4d4", "5d4", "5d4"
  ]
}

(我不习惯格式化GStrings,所以可能不正确,在这种情况下我道歉)

一个Map额外的成员(例如"grenadeDamage"在上面的 Json 文件中)将包含他们的名字到他们各自List的 s 的映射。然后这些将被传递到这样布局的类中:

class Gun
{
    private int level
    // ... (additional fields)
    private List<?> description
    private Map<String, List<String>> variables

    // ... (Constructors, methods, etc.)

    List<String> getDescription()
    {
        description
    }
}

这里的希望是在运行时调用时将GStrings 中的任何 s转换List为常规Strings 。getDescription()

在给定的示例中,如果level字段为 7,则 的第 5 行将description填充${variables}.get('grenadeDamage').get(${level})到 value1d8或 中的第 7 项grenadeDamage

这样做的问题是,当使用 aJsonSlurper来解析 Json 文件时,description会直接创建为 a List<String>。理想情况下,我想在description不立即将类型强制为String.

欢迎和赞赏任何意见、建议和建设性批评!

4

0 回答 0