如何在 groovy 中使用 JSON slurper 动态获取 JSON 内容值

How to fetch JSON content values dynamically using JSON slurper in groovy

我试图通过动态传递几个参数来过滤掉 JSON 响应值。下面是代码。

import com.eviware.soapui.support.XmlHolder
import groovy.json.JsonSlurper

responseContent = testRunner.testCase.getTestStepByName("Request 1").getPropertyValue("Response")
jsonresponse = new JsonSlurper().parseText(responseContent)

log.info jsonresponse.context["parameter"]

context["parameter"] 存储来自 Excel sheet 的值,并且只对 context["parameter"] 执行 log.info,它会正确显示这些值。下面是代码 log.info context["parameter"].

的输出
Thu Dec 14 07:18:53 CST 2017:INFO:firstName
Thu Dec 14 07:18:53 CST 2017:INFO:lastName
Thu Dec 14 07:18:54 CST 2017:INFO:frNum
Thu Dec 14 07:18:54 CST 2017:INFO:notes 

但是当我执行代码时

log.info jsonresponse.context["parameter"]

结果:

Thu Dec 14 07:20:41 CST 2017:INFO:[]

Thu Dec 14 07:20:41 CST 2017:INFO:[]

Thu Dec 14 07:20:41 CST 2017:INFO:[]

Thu Dec 14 07:20:42 CST 2017:INFO:[]

Thu Dec 14 07:20:42 CST 2017:INFO:[]

log.info jsonresponse 产生以下响应,我需要从中获取某些值(存储在 context["parameter"] 中的值)

Thu Dec 14 07:21:45 CST 2017:INFO:[{errorMessage=null, middleName=null, lastName=Kosnick, frName=Kosnick Steven H , mplastName=null, competeRgnTxt=null, doNum=null, gddUnitStDate=null, fdNum=null, mpfirstName=null, legalEntityID=null, noNum=null, contractType=Financial Rep, frNum=046426, offcNum=NO 091, notes=Active, fullTmeSrvDt=null, firstName=Steven, networkOfficeNum=091}]

而不是这个:

log.info jsonresponse.context["parameter"]

试试这个:

log.info jsonresponse[context["parameter"]]

这应该可以解决您的问题:)