gpt4 book ai didi

groovy - SOAPUI 中是否有用于基本 JSON 路径表达式的子字符串方法?

转载 作者:行者123 更新时间:2023-12-01 13:42:12 24 4
gpt4 key购买 nike

你过去帮了我大忙,所以我希望你能帮我解决这个问题。所以目前我正在做一个项目,使用 soapui 来取回一个巨大的 Json 有效负载。我需要创建一些断言,其中一些需要查看只有一个共同点的多个节点。那一件事是其中一个节点的第一部分。

所以我正在寻找的是某种用于 JSONPath 的子字符串命令。这是我正在寻找的示例。

"BurgerJoints":    [
{
"JointName": "Bob's Burgers",
"Price": 5
},
{
"JointName": "Bob's Broiler Stand",
"Price": 5
},
{
"JointName": "Burger King",
"Price": 5
},
{
"JointName": "Bob's Beef Haven",
"Price": 5
},
{
"JointName": "Super Weenie Hut",
"Price": 5
}
]

在我的示例中,假设我正在寻找属于 Bob 的所有关节。所以我最初的想法是做类似的事情 BurgerJoints[?(@.Substring(JointName,0,3)==“鲍勃”)], 给我节点。但它看起来没有用。谁能告诉我我的语法哪里出错了,或者如果没有办法那样做,实现我的目标的最佳方法是什么?

谢谢大家!!

编辑:

所以我尝试使用 Groovyscript 来完成它,我想我已经接近了,但是在某个地方列表没有填充。这是我正在使用的代码

//imports
import groovy.json.JsonSlurper

//grab the response
def ResponseMessage = messageExchange.response.responseContent
//define a JsonSlurper
def jsonSlurper = new JsonSlurper().parseText(ResponseMessage)

//verify the slurper isn't empty
assert !(jsonSlurper.isEmpty())

def jsonlist =[]

def i = 0

while (jsonSlurper.BurgerJoints[i] != null)
{
if(jsonSlurper.BurgerJoints[i].JointName.toString().substring(0,3)=="Bob")
{
jsonlist.add(jsonSlurper.BurgerJoints[i])

}

i++
}


def jsonlist2 = new JsonSlurper().parseText(jsonlist.toListString())

assert jsonlist2.size()==3

不幸的是仍然没有工作。

最佳答案

这是 Groovy 脚本,它断言 JointName 的列表包含值为 3 的值 Bob's。请找到内联注释。

import groovy.json.JsonSlurper
//Defining json string a fixed value. Of course, you can use dynamic value
//using messageExchange like you shown in your question.
def jsonString = '''
{
"BurgerJoints": [
{
"JointName": "Bob's Burgers",
"Price": 5
},
{
"JointName": "Bob's Broiler Stand",
"Price": 5
},
{
"JointName": "Burger King",
"Price": 5
},
{
"JointName": "Bob's Beef Haven",
"Price": 5
},
{
"JointName": "Super Weenie Hut",
"Price": 5
}
]
}
'''
//Parse the string, create slurper object
def json = new JsonSlurper().parseText(jsonString)
//find all JointNames which contains Bob's, and apply size, and assert with 3
assert json.BurgerJoints.findAll { it.JointName.contains('Bob\'s')}.size() == 3

更新:根据评论,添加有助于实现此问题作者所寻找内容的附加语句。

def jointList = json.BurgerJoints.findAll { it.JointName.contains('Bob\'s')}
log.info jointList

更新 2: 根据另一条评论,验证 jointList

中第一和第二的价格是否相等
assert jointList[0].Price == jointList[1].Price

关于groovy - SOAPUI 中是否有用于基本 JSON 路径表达式的子字符串方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39105381/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com