0

Lists を含む GroovyのJson ファイルを解析しようとしていますString。これらの文字列の一部にはフォーマットが含まれているため、これらを通常の s ではなく sGStringとして解析したいと考えていました。GStringString

たとえばdescription、次のメンバーには 1GStringと 7が含まれますString

{
  ... (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"
  ]
}

(sの整形に慣れていないGStringので、間違っているかもしれませんが、その場合はお詫びします)

Map追加メンバー (上記の Json ファイルなど)"grenadeDamage"の には、それぞれListの への名前のマッピングが含まれます。これらは、次のようにレイアウトされたクラスに渡されます。

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

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

    List<String> getDescription()
    {
        description
    }
}

ここでの希望は、 が実行時に呼び出されたときに、 内のすべてGStringListを通常の に変換することです。StringgetDescription()

与えられた例では、levelフィールドが 7 の場合、 の 5 行目description${variables}.get('grenadeDamage').get(${level})値が入力される1d8か、 の 7 番目の項目が入力されgrenadeDamageます。

これに関する問題はJsonSlurper、Json ファイルを解析するために a を使用する場合、が直接descriptionとして作成されることです。理想的には、タイプ as をすぐに強制せずList<String>に解析したいと思います。descriptionString

コメント、アドバイス、建設的な批判は大歓迎です。

4

0 に答える 0