在 Groovyshell 中执行变量代码
Executing variable code in Groovyshell
考虑这段代码
def RespJson = RespSlurper.parseText(content)
def RespNode= "RespJson"+"."+ assertionKey
其中 assertionKey
将在每次迭代中动态变化,并且将具有类似 seatbid[0].bid[0].impid
的值
如何在 Groovyshell 中执行以下代码,我正在尝试这个
def v
def a = new Binding(RespJson: RespJson)
new GroovyShell(a).evaluate(" v=${RespNode}")
log.info(v)
但我得到 v 的值为 null
。任何帮助表示赞赏。谢谢。
编辑:
def RespSlurper = new JsonSlurper()
def content = step.testRequest.response.responseContent
content
的值为
{
"seatbid":[
{
"bid":[
{
"id":"1",
"impid":"1",
"price":3.5999999046325684,
"nurl":"http:...",
"adomain":[
"zagg.com",
"zagg.com"
],
"iurl":"http:...",
"crid":"30364.s320x50m",
"h":0,
"w":0
}
],
"group":0
}
],
"cur":"USD",
"nbr":0
}
我有下面的代码,因为我认为这是问题所问内容的浓缩版。
在这种情况下,似乎可以从绑定中检索 v
变量,即 a
。绑定的变量在 variables
对象上可用。
此外,由于 GroovyShell 评估的脚本与 v
设置的相同,打印 GroovyShell 对象的输出也将打印“1”。
import groovy.json.JsonSlurper
def RespSlurper = new JsonSlurper()
def content = '{"seatbid":[{"bid":[{"id":"1","impid":"1","price":3.5999999046325684,"nurl":"http:...","adomain":["zagg.com","zagg.com"],"iurl":"http:...","crid":"30364.s320x50m","h":0,"w":0}],"group":0}],"cur":"USD","nbr":0}'
def RespJson = RespSlurper.parseText(content)
def assertionKey = "seatbid[0].bid[0].impid"
def RespNode= "RespJson"+"."+ assertionKey
def v
def a = new Binding(RespJson: RespJson)
def result = new GroovyShell(a).evaluate("v=${RespNode}")
println(v)
// Important addition!
println(result) <=== print the value of the GroovyShell, it will show "1"
println(a.variables.v) <=== retrieve the "v" variable off of the binding, it will show "1"
考虑这段代码
def RespJson = RespSlurper.parseText(content)
def RespNode= "RespJson"+"."+ assertionKey
其中 assertionKey
将在每次迭代中动态变化,并且将具有类似 seatbid[0].bid[0].impid
如何在 Groovyshell 中执行以下代码,我正在尝试这个
def v
def a = new Binding(RespJson: RespJson)
new GroovyShell(a).evaluate(" v=${RespNode}")
log.info(v)
但我得到 v 的值为 null
。任何帮助表示赞赏。谢谢。
编辑:
def RespSlurper = new JsonSlurper()
def content = step.testRequest.response.responseContent
content
的值为
{
"seatbid":[
{
"bid":[
{
"id":"1",
"impid":"1",
"price":3.5999999046325684,
"nurl":"http:...",
"adomain":[
"zagg.com",
"zagg.com"
],
"iurl":"http:...",
"crid":"30364.s320x50m",
"h":0,
"w":0
}
],
"group":0
}
],
"cur":"USD",
"nbr":0
}
我有下面的代码,因为我认为这是问题所问内容的浓缩版。
在这种情况下,似乎可以从绑定中检索 v
变量,即 a
。绑定的变量在 variables
对象上可用。
此外,由于 GroovyShell 评估的脚本与 v
设置的相同,打印 GroovyShell 对象的输出也将打印“1”。
import groovy.json.JsonSlurper
def RespSlurper = new JsonSlurper()
def content = '{"seatbid":[{"bid":[{"id":"1","impid":"1","price":3.5999999046325684,"nurl":"http:...","adomain":["zagg.com","zagg.com"],"iurl":"http:...","crid":"30364.s320x50m","h":0,"w":0}],"group":0}],"cur":"USD","nbr":0}'
def RespJson = RespSlurper.parseText(content)
def assertionKey = "seatbid[0].bid[0].impid"
def RespNode= "RespJson"+"."+ assertionKey
def v
def a = new Binding(RespJson: RespJson)
def result = new GroovyShell(a).evaluate("v=${RespNode}")
println(v)
// Important addition!
println(result) <=== print the value of the GroovyShell, it will show "1"
println(a.variables.v) <=== retrieve the "v" variable off of the binding, it will show "1"