【问题标题】:How to display list values of JSON attribute using groovy script in soap ui如何在soap ui中使用groovy脚本显示JSON属性的列表值
【发布时间】:2018-11-28 05:31:05
【问题描述】:

我有以下 json,

"Location": "abc",
"Codes":    
[
  {
    "high": "xyz",
    "low": "aaa"
  }
]

我正在使用 SOAP UI 进行数据驱动测试。在上面的代码中,我使用下面的代码在 groovy 脚本中显示“位置”属性 json 值

def jsonRes = slurper.parseText(responseJson)
def String LocationJson = jsonRes.Location
log.info ("location is " +LocationJson)

谁能建议我如何在“代码”列表中显示“高”和“低”的 json 值?

【问题讨论】:

    标签: json soapui data-driven-tests data-driven


    【解决方案1】:

    “[”和“]”用于创建数组。因此,您将需要使用 [0] 来访问该数组的第一个元素。或者使用循环结构,让您可以直接处理数组的每个元素,就像我在下面的示例中所做的那样。

    我修改了你的代码。我希望你能够在不修改的情况下运行它。

    def jsonstring = '{"Location": "abc","Codes": [ { "high": "xyz", "low": "aaa" } ] }"'
    log.info jsonstring
    def slurper = new groovy.json.JsonSlurper()
    def jsonRes = slurper.parseText(jsonstring)
    def LocationJson = jsonRes.Location
    log.info ("location is " +LocationJson)
    
    // this will loop through all Codes element...
    for (def codeElement : jsonRes.Codes) {
        log.info ("high is " + codeElement.high)
        log.info ("low is " + codeElement.low)  
    }
    

    【讨论】:

    • ...如果您不需要循环,并且只想直接访问元素,这可以通过 jsonRes.Codes[0].high 实现
    猜你喜欢
    • 1970-01-01
    • 2017-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多