3

考虑这段代码

 def RespJson = RespSlurper.parseText(content)    
 def RespNode= "RespJson"+"."+ assertionKey

whereassertionKey将在每次迭代中动态变化,并将具有如下值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
}
4

1 回答 1

1

我有下面的代码,因为我认为这是问题所问内容的浓缩版本。

在这种情况下,似乎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"
于 2015-01-21T19:54:20.280 回答