如何在 JIRA Scriptrunner 中使用 Groovy 中的给定值从嵌套映射 (JSON) 获取密钥

How to get the key from a nested map (JSON) using given value in Groovy in JIRA Scriptrunner

希望这个问题能让大家身体健康。

根据标题,想知道 Groovy 中是如何完成的。找了几个,比如this,但是问答没有帮助。

JSON是这样的

def ​json = '''{
     "boston": [
{
  "name":"bob",
  "phone":"242 123123",
},
{
  "name":"alice",
  "phone":"212-123-345",
}
],
"chicago": [
{
  "name":"charlie",
  "phone":"313-232-545",
},
{
  "name":"denise",
  "phone":"414-123-546",
}
]
}'''

但是我如何使用该值,例如 bob 得到 boston?

当您使用 parsedjson['chicago']['email'] 时,结果将是

[charlie@chicago.com, denise@chicago.com]

我尝试做类似

的事情
def getKey = parsedjson['email']?.key

按照建议 here 但在 JIRA ScriptRunner 控制台中返回 null

提前不胜感激!

parsedjson['email']?.key 返回 null 因为 key 不是 List 方法。 key 是一种 Entry 方法,因此要从值中找到键,您必须遍历地图的条目集。

下面是一个使用 Map.find 从人名中获取城市的示例,其中 returns 和 Entry:

parsedjson.find { it.value.find { it["name"] == "bob" } }.key