使用正则表达式查找 JSON 中的键并将它们放入 java 中的映射中

Find keys in JSON with regex and put them in map in java

我有 JSON 来自 API 类似于:

{
  "something": "something",
  "example_1": true,
  "something2": "something",
  "example_2": false,
  "example_3": true
}

我想将所有 example_x 键及其值放在一个映射中。我猜这是可能的正则表达式,但我不知道该怎么做。

我知道 example_\d 是正则表达式,仅此而已。我没有找到任何类似的问题,但我可能使用了错误的搜索词。

信息:我使用 org.json 库获取 JSON 对象,但如果它有效,我可以将其更改为另一个库。

任何帮助将不胜感激

得到它的工作:(感谢 tgdavies 告诉我使用迭代器)

//jsonObject is the json in the question for example
HashMap Map<String, Boolean> = new HashMap<>();

Iterator<String> keys = jsonObject.keys();
while (keys.hasNext()) {
    String key = keys.next();
    if (key.matches("example_\d+")) {
        Map.put(key, jsonObject.getBoolean(key));
    }
}