Json 中显示的属性不正确
Incorrect properties displayed from Json
我在尝试使用 SOAP UI 和 groovy 脚本从 json 收集数据时遇到问题。下面是一个例子 json:
{
"regions": [{
"hotels": [{
"roomInformation": [{
"hotelRoomId": xxx,
}],
"regionId": x,
"hotelId": xxx,
"providerInformation": {
"ratePlanCode": "xxx",
},
"providerHotelId": 0000001
},
{
"roomInformation": [{
"hotelRoomId": xx,
}],
"regionId": x,
"hotelId": xxx,
"providerInformation": {
"ratePlanCode": "ggg",
},
"providerHotelId": 0000002
}
],
"errors": null
}],
"errors": null
}
我要做的是selectproviderHotelId
和ratePlanCode
的第一个实例。为此,我使用下面的 groovy 脚本来解决这个问题:
def alert = com.eviware.soapui.support.UISupport
import groovy.json.JsonSlurper
def response = testRunner.testCase.getTestStepByName("Search Test").getProperty("Response").getValue();
def jsonRes = new JsonSlurper().parseText(response);
def providerhotelid = jsonRes.regions.hotels.providerHotelId[0].toString()
def rateplancode = jsonRes.regions.hotels.providerInformation[0].ratePlanCode.toString()
log.info providerhotelid
testRunner.testCase.setPropertyValue('providerhotelid', providerhotelid)
testRunner.testCase.setPropertyValue('rateplancode', rateplancode)
这在我的自定义属性中输出如下:
- providerhotelid - [0000001,0000002]
- 费率计划代码 - [xxx]
以上是不正确的,因为:
- providerhotelid - 当我只想要第一个应该是
0000001
. 时,它会显示所有提供商酒店 ID
- rateplancode - 是正确的,但它周围显示了一个 [],我希望将其删除。 providerhotelid 也是如此。
所以对于这个例子,我的自定义属性应该显示:
- 供应商酒店编号 - 0000001
- 费率计划代码 - xxx
如何在我的 groovy 脚本中实现这一点?
这是您需要的:
//Get all the values, falatten them and get the first one
def providerhotelid = jsonRes.regions.hotels.providerHotelId.flatten()[0]
def rateplancode = jsonRes.regions.hotels.providerInformation.ratePlanCode.flatten()[0]
log.info providerhotelid
log.info rateplancode
您可以快速在线试用Demo
我在尝试使用 SOAP UI 和 groovy 脚本从 json 收集数据时遇到问题。下面是一个例子 json:
{
"regions": [{
"hotels": [{
"roomInformation": [{
"hotelRoomId": xxx,
}],
"regionId": x,
"hotelId": xxx,
"providerInformation": {
"ratePlanCode": "xxx",
},
"providerHotelId": 0000001
},
{
"roomInformation": [{
"hotelRoomId": xx,
}],
"regionId": x,
"hotelId": xxx,
"providerInformation": {
"ratePlanCode": "ggg",
},
"providerHotelId": 0000002
}
],
"errors": null
}],
"errors": null
}
我要做的是selectproviderHotelId
和ratePlanCode
的第一个实例。为此,我使用下面的 groovy 脚本来解决这个问题:
def alert = com.eviware.soapui.support.UISupport
import groovy.json.JsonSlurper
def response = testRunner.testCase.getTestStepByName("Search Test").getProperty("Response").getValue();
def jsonRes = new JsonSlurper().parseText(response);
def providerhotelid = jsonRes.regions.hotels.providerHotelId[0].toString()
def rateplancode = jsonRes.regions.hotels.providerInformation[0].ratePlanCode.toString()
log.info providerhotelid
testRunner.testCase.setPropertyValue('providerhotelid', providerhotelid)
testRunner.testCase.setPropertyValue('rateplancode', rateplancode)
这在我的自定义属性中输出如下:
- providerhotelid - [0000001,0000002]
- 费率计划代码 - [xxx]
以上是不正确的,因为:
- providerhotelid - 当我只想要第一个应该是
0000001
. 时,它会显示所有提供商酒店 ID
- rateplancode - 是正确的,但它周围显示了一个 [],我希望将其删除。 providerhotelid 也是如此。
所以对于这个例子,我的自定义属性应该显示:
- 供应商酒店编号 - 0000001
- 费率计划代码 - xxx
如何在我的 groovy 脚本中实现这一点?
这是您需要的:
//Get all the values, falatten them and get the first one
def providerhotelid = jsonRes.regions.hotels.providerHotelId.flatten()[0]
def rateplancode = jsonRes.regions.hotels.providerInformation.ratePlanCode.flatten()[0]
log.info providerhotelid
log.info rateplancode
您可以快速在线试用Demo