如何计算汤UI中json响应中标记的出现次数?使用断言

时间:2018-04-15 05:55:33

标签: groovy soapui assertions

我的json回复

{
  "planList": [
    {
      "planCode": "xx",
      "statusCode": "0"
    },
    {
      "planCode": "yy",
      "statusCode": "0",
      "statusMessage": "Success"
    },
    {
      "planCode": "zz",
      "statusCode": "0"
    }
  ]
}

现在我想计算plancode的出现次数。

1 个答案:

答案 0 :(得分:0)

def json  = new groovy.json.JsonSlurper().parseText("""    {
      "planList": [
        {
          "planCode": "xx",
          "statusCode": "0"
        },
        {
          "planCode": "yy",
          "statusCode": "0",
          "statusMessage": "Success"
        },
        {
          "planCode": "zz",
          "statusCode": "0"
        }
      ]
    }
    """)

println json.planList*.planCode                 // all planCodes
println json.planList*.planCode.size()          // all planCodes' count
println json.planList*.planCode.unique()        // uniq planCodes
println json.planList*.planCode.unique().size() // uniq planCodes's count