groovy - 如何使用 Script runner groovy 脚本获取数组项
问题描述
我在 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 表示 Array 对象的单个用户输出。
仅检索此返回 Array 的Id部分的方法是什么?
我已经尝试直接使用以下方法从我的方法返回:
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
当前读取的字符是 'b',int 值为 98,需要 '}' 或 '',但当前字符 'b' 的 int 值为 98 行号 1 索引号 2
==== 更新 2 ===
如果我将我的方法 json 响应更改为:
response.success = { resp, json ->
_userId=json["value"].toString()
}
返回值为json:
String json=apiHelper.getGuestUserId(apiHelper.Token,email)
返回如下(注意没有{})
[[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()
然后它失败了,与我最初的帖子相同,因为它不是 Json 格式的返回,而是一个数组项。
知道如何根据上面的返回字符串让它工作吗?
解决方案
如果您需要获取所有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()
}
}
推荐阅读
- flutter - 列表
到地图 在 Dart 到 Firestore - python - 将 YUV_NV12 转换为 YUV444
- http-live-streaming - Exoplayer 播放时边缘粗糙
- vue.js - 使用 Vue CLI V4 调试 Vue 3
- css - WYSIWYG ACF 字段似乎采用了来自周围 div 的 css 样式
- azure - 在 Azure 存储数据湖中保存 JSON 文件以 NUL (Ascii 0) 字符和空格结束
- javascript - 如何以角度为 setTimeout 函数和 activeElement 编写单元测试?
- vue.js - 带有 Jitsi 或 Zoom 的 Vue3
- reactjs - react.js 中反应组件样式的语法
- javascript - 允许用户写出他想输入多少个输入