正在解析 Groovy 中的 JSON 个对象
Parsing JSON object in Groovy
我正在尝试编写一个 Groovy 脚本来执行 REST api 调用并获取一个 JSON 对象,然后,我需要从这个 JSON 并检查它是否与我在脚本中提供的另一个字符串匹配。
直到比较部分我都做了,
我从 JSON 得到的字符串看起来像
[AAAAAA/BBBBBB/CCCCCC/file.txt]
这是我的 Groovy 脚本:
/*Import Section*/
//--------------//
groovy.json.*
/*Var Declaration*/
//---------------//
String errMessage = "There were no Junit tests impacted in this PR,
Comparison is: "
String scssMessage = "There were Junit tests impacted in this PR,
Comparison is: "
usr= "USERNAME"
pass= "PASSWORD"
pr_num = 92
String validPath = "AAAAAA/BBBBBB/CCCCCC/DDDDDD/EEEEEE"
def copmarison = !false
/*REST API call*/
//------------//
url= "http://{$usr}:
{$pass}@XX.XXX.X.XX:PPPP/rest/api/1.0/projects/XX/repos/TTTTT/pull-
requests/+{$pr_num}/changes?changescope"
process = [ 'bash', '-c', "curl ${url}" ].execute()
process.waitFor()
/*JSON parsing*/
//------------//
def info = new JsonSlurper().parseText(process.text)
def path = info.values.path.toString
/*Impacted JUNIT verifycation*/
//---------------------------//
if(path==validPath){
println ("$scssMessage"+"Valid"+"\n")
comparison=true
}else{
println ("$errMessage"+"Not Valid"+"\n")
comparison=!false
}
我确定我的比较不好,我想比较并找到
如果我的 "path" 的一部分包含在我的 "validPath".
中
例如,以下情况表示为真:
AAAAAA/BBBBBB/CCCCCC/file.txt
包含在:
AAAAAA/BBBBBB/CCCCCC/DDDDDD/EEEEEE
我需要找到一种方法来进行这种比较
请帮忙
如果我正确理解你的问题,你需要检查的是文件的路径部分与特定路径的对比。在这种情况下,这将起作用:
// you should already have these; including here for clarity
def file = "AAAAAA/BBBBBB/CCCCCC/file.txt"
def path = "AAAAAA/BBBBBB/CCCCCC/DDDDDD/EEEEEE"
return path.contains(file[0..file.lastIndexOf("/")])
我正在尝试编写一个 Groovy 脚本来执行 REST api 调用并获取一个 JSON 对象,然后,我需要从这个 JSON 并检查它是否与我在脚本中提供的另一个字符串匹配。
直到比较部分我都做了,
我从 JSON 得到的字符串看起来像
[AAAAAA/BBBBBB/CCCCCC/file.txt]
这是我的 Groovy 脚本:
/*Import Section*/
//--------------//
groovy.json.*
/*Var Declaration*/
//---------------//
String errMessage = "There were no Junit tests impacted in this PR,
Comparison is: "
String scssMessage = "There were Junit tests impacted in this PR,
Comparison is: "
usr= "USERNAME"
pass= "PASSWORD"
pr_num = 92
String validPath = "AAAAAA/BBBBBB/CCCCCC/DDDDDD/EEEEEE"
def copmarison = !false
/*REST API call*/
//------------//
url= "http://{$usr}:
{$pass}@XX.XXX.X.XX:PPPP/rest/api/1.0/projects/XX/repos/TTTTT/pull-
requests/+{$pr_num}/changes?changescope"
process = [ 'bash', '-c', "curl ${url}" ].execute()
process.waitFor()
/*JSON parsing*/
//------------//
def info = new JsonSlurper().parseText(process.text)
def path = info.values.path.toString
/*Impacted JUNIT verifycation*/
//---------------------------//
if(path==validPath){
println ("$scssMessage"+"Valid"+"\n")
comparison=true
}else{
println ("$errMessage"+"Not Valid"+"\n")
comparison=!false
}
我确定我的比较不好,我想比较并找到 如果我的 "path" 的一部分包含在我的 "validPath".
中例如,以下情况表示为真:
AAAAAA/BBBBBB/CCCCCC/file.txt
包含在:
AAAAAA/BBBBBB/CCCCCC/DDDDDD/EEEEEE
我需要找到一种方法来进行这种比较
请帮忙
如果我正确理解你的问题,你需要检查的是文件的路径部分与特定路径的对比。在这种情况下,这将起作用:
// you should already have these; including here for clarity
def file = "AAAAAA/BBBBBB/CCCCCC/file.txt"
def path = "AAAAAA/BBBBBB/CCCCCC/DDDDDD/EEEEEE"
return path.contains(file[0..file.lastIndexOf("/")])