SOAPUI 中是否有用于基本 JSON 路径表达式的子字符串方法?
Is there a substring method for basic JSON Path expressions in SOAPUI?
你过去帮了我大忙,所以我希望你能帮我解决这个问题。所以目前我正在做一个项目,使用 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)=="Bob")]
, 给我节点。但它看起来没有用。任何人都可以告诉我我的语法哪里出错了,或者如果没有办法那样做,实现我的目标的最佳方法是什么?
谢谢大家!!
编辑:
所以我尝试使用 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
的列表包含值 Bob's
和值 3。请找到内联注释。
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
你过去帮了我大忙,所以我希望你能帮我解决这个问题。所以目前我正在做一个项目,使用 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)=="Bob")] , 给我节点。但它看起来没有用。任何人都可以告诉我我的语法哪里出错了,或者如果没有办法那样做,实现我的目标的最佳方法是什么?
谢谢大家!!
编辑:
所以我尝试使用 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
的列表包含值 Bob's
和值 3。请找到内联注释。
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