问题描述
检查每个json响应值实例的最动态方法是否与脚本断言中的另一个json响应值匹配? 我的意思是让我说我有以下回应:
{
xxx:[{
roomInformation:[ {
xxx:xxx
}],
总价:xxx
},
$ b房间信息:[{
xxx:xxx
}],
totalPrice:xxx
}
]
}
我想检查第一个房间的价格是否与第一个 totalPrice
和第二个 roomPrice
以匹配第二个 totalPrice
。它必须是动态的,因为我可能会得到许多不同的实例,所以我不能简单地用[0]和[1]来查看json。实际检查每个 roomPrice
与其相应的 totalPrice
匹配。
谢谢
这是脚本断言
来检查每个 roomPrice
匹配或不匹配 totalPrice
。
编辑:based关于OP提供的全部回复
脚本声明:
//检查响应是否为空
assert context.response ,响应为空或为空
def json = new groovy.json.JsonSlurper()。parseText(context.response)
def sb = new StringBuffer()
json .regions.each {region - >
region.hotels.each {hotel - >
(hotel?.totalPrice == hotel?.roomInformation [0] ?. roomPrice)?:sb.append(房间价格$ {hotel?.roomInformation [0] ?. roomPrice}与总价格不匹配(sb.toString()){
抛出新的错误(sb.toString())
} $ {$ hotel.totalPrice})
}
}
} else {log.info'Prices match'}
What is the most dynamic way of checking each instance of a json response value matches another json response value within a script assertion?
What I mean is lets say I have the following response below:
{
"xxx": [{
"roomInformation": [{
"xxx": xxx
}],
"totalPrice": xxx
},
{
"roomInformation": [{
xxx: xxx
}],
"totalPrice": xxx
}
]
}
I want to check that the first room price to match with the first totalPrice
and the second roomPrice
to match with the second totalPrice
. It has to be dynamic as I may get many different instances of this so I can't simply just look through the json with [0] and [1]. Virtually check each roomPrice
matches with its corresponding totalPrice
.
Thanks
Here is the script assertion
to check each roomPrice
is matching or not with totalPrice
.
EDIT: based on OP's full response provided here
Script Assertion:
//Check if the response is not empty
assert context.response, "Response is empty or null"
def json = new groovy.json.JsonSlurper().parseText(context.response)
def sb = new StringBuffer()
json.regions.each { region ->
region.hotels.each { hotel ->
(hotel?.totalPrice == hotel?.roomInformation[0]?.roomPrice) ?: sb.append("Room price ${hotel?.roomInformation[0]?.roomPrice} is not matching with total price ${hotel.totalPrice}")
}
}
if (sb.toString()) {
throw new Error(sb.toString())
} else { log.info 'Prices match' }
这篇关于如何检查一个值与json响应中的另一个correpsonding值相匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!