如何使用 Script runner groovy 脚本获取数组项
How to get an Array item using Script runner groovy script
我在 Jira 的脚本运行程序中使用 MSGraph API 和 groovy 脚本,以便通过他的电子邮件地址检索 guets AD 用户。
我使用的代码如下:
public String getGuestUserId(String AuthToken,String userEmail){
String _userId
def http = new HTTPBuilder(graph_base_user_url + "?")
http.request(GET) {
requestContentType = ContentType.JSON
//uri.query = [ $filter:"mail eq '$userEmail'"].toString()
uri.query=[$filter:"mail eq '$userEmail'"]
headers.'Authorization' = "Bearer " + AuthToken
response.success = { resp, json ->
_userId=json["value"]
}
// user ID not found : error 404
response.'404' = { resp ->
_userId = 'Not Found'
}
}
_userId
}
这个调用的输出如下:
[{businessPhones=[], displayName=user test, givenName=null, jobTitle=null,
mail=user1@gmail.com, mobilePhone=null, officeLocation=null, preferredLanguage=null,
surname=null, userPrincipalName=user1_gmail.com#EXT#@rlxcom.onmicrosoft.com, id=7982c558-
ba50-4380-9e94-114d8b340720}]
IT 表示数组对象的单个用户输出。
仅检索此 return 数组的 Id 部分的方法是什么?
我已经尝试 return 从我的方法直接使用 :
response.success = { resp, json ->
_userId=json["value"][0]["Id"]
但是没用
==> 已更新
如果我只是使用以下代码部分来查看解析 Json 是否正常,我会收到异常错误:
def json = new groovy.json.JsonSlurper().parseText ret
return json
groovy.json.JsonException: 期待 '}' 或 ',' 但得到的当前 char 'b' 的 int 值为 98
The current character read is 'b' with an int value of 98
expecting '}' or ',' but got current char 'b' with an int value of 98
line number 1
index number 2
==== 更新 2 ===
如果我将方法 json 响应更改为:
response.success = { resp, json ->
_userId=json["value"].toString()
}
return值为json:
String json=apiHelper.getGuestUserId(apiHelper.Token,email)
是return如下(注意没有{})
[[businessPhones:[], displayName:serge cal test, givenName:null,
jobTitle:null, mail:calderara.serge@gmail.com, mobilePhone:null,
officeLocation:null, preferredLanguage:null, surname:null,
userPrincipalName:calderara.serge_gmail.com#EXT#@rlxcom.onmicrosoft.com,
id:7982c558-ba50-4380-9e94-114d8b340720]]
调用然后根据您的示例解析方法如下:
def retVal = new groovy.json.JsonSlurper().parseText (json)
return retVal.id.first()
然后它失败了,与我的初始 post 相同,因为它不是 Json 格式 return,而是数组项。
知道如何根据上面的 return 字符串让它工作吗?
如果需要获取所有id(通用方案):
String str = """{"value":[{"businessPhones":"[]", "displayName":"user test", "givenName":null, "jobTitle":null,
"mail":"user1@gmail.com", "mobilePhone":null, "officeLocation":null, "preferredLanguage":null, "surname":null,
"userPrincipalName":"user1_gmail.com#EXT#@rlxcom.onmicrosoft.com", "id":"7982c558-ba50-4380-9e94-114d8b340720"}]}"""
def json = new groovy.json.JsonSlurper().parseText str
def ids = json.value*.id
assert ['7982c558-ba50-4380-9e94-114d8b340720'] == ids
您的具体案例:
http.request(GET) {
//...
response.success = { resp, json ->
_userId = json.value[ 0 ].id
// or
_userId=json.value*.id.first()
}
}
我在 Jira 的脚本运行程序中使用 MSGraph API 和 groovy 脚本,以便通过他的电子邮件地址检索 guets AD 用户。
我使用的代码如下:
public String getGuestUserId(String AuthToken,String userEmail){
String _userId
def http = new HTTPBuilder(graph_base_user_url + "?")
http.request(GET) {
requestContentType = ContentType.JSON
//uri.query = [ $filter:"mail eq '$userEmail'"].toString()
uri.query=[$filter:"mail eq '$userEmail'"]
headers.'Authorization' = "Bearer " + AuthToken
response.success = { resp, json ->
_userId=json["value"]
}
// user ID not found : error 404
response.'404' = { resp ->
_userId = 'Not Found'
}
}
_userId
}
这个调用的输出如下:
[{businessPhones=[], displayName=user test, givenName=null, jobTitle=null,
mail=user1@gmail.com, mobilePhone=null, officeLocation=null, preferredLanguage=null,
surname=null, userPrincipalName=user1_gmail.com#EXT#@rlxcom.onmicrosoft.com, id=7982c558-
ba50-4380-9e94-114d8b340720}]
IT 表示数组对象的单个用户输出。
仅检索此 return 数组的 Id 部分的方法是什么?
我已经尝试 return 从我的方法直接使用 :
response.success = { resp, json ->
_userId=json["value"][0]["Id"]
但是没用
==> 已更新
如果我只是使用以下代码部分来查看解析 Json 是否正常,我会收到异常错误:
def json = new groovy.json.JsonSlurper().parseText ret
return json
groovy.json.JsonException: 期待 '}' 或 ',' 但得到的当前 char 'b' 的 int 值为 98
The current character read is 'b' with an int value of 98 expecting '}' or ',' but got current char 'b' with an int value of 98 line number 1 index number 2
==== 更新 2 ===
如果我将方法 json 响应更改为:
response.success = { resp, json ->
_userId=json["value"].toString()
}
return值为json:
String json=apiHelper.getGuestUserId(apiHelper.Token,email)
是return如下(注意没有{})
[[businessPhones:[], displayName:serge cal test, givenName:null,
jobTitle:null, mail:calderara.serge@gmail.com, mobilePhone:null,
officeLocation:null, preferredLanguage:null, surname:null,
userPrincipalName:calderara.serge_gmail.com#EXT#@rlxcom.onmicrosoft.com,
id:7982c558-ba50-4380-9e94-114d8b340720]]
调用然后根据您的示例解析方法如下:
def retVal = new groovy.json.JsonSlurper().parseText (json)
return retVal.id.first()
然后它失败了,与我的初始 post 相同,因为它不是 Json 格式 return,而是数组项。
知道如何根据上面的 return 字符串让它工作吗?
如果需要获取所有id(通用方案):
String str = """{"value":[{"businessPhones":"[]", "displayName":"user test", "givenName":null, "jobTitle":null,
"mail":"user1@gmail.com", "mobilePhone":null, "officeLocation":null, "preferredLanguage":null, "surname":null,
"userPrincipalName":"user1_gmail.com#EXT#@rlxcom.onmicrosoft.com", "id":"7982c558-ba50-4380-9e94-114d8b340720"}]}"""
def json = new groovy.json.JsonSlurper().parseText str
def ids = json.value*.id
assert ['7982c558-ba50-4380-9e94-114d8b340720'] == ids
您的具体案例:
http.request(GET) {
//...
response.success = { resp, json ->
_userId = json.value[ 0 ].id
// or
_userId=json.value*.id.first()
}
}