SOAPUI:如何在JSON响应中查找节点数 [英] SOAPUI: How to find node count in JSON response
问题描述
我在SOAP UI中具有以下JSON响应:- 我需要在此JSON中找到JSON节点数. 我尝试使用JSONPATH Count,但是无法给出准确的结果. 在这里,期望有8个节点作为输出.
I have JSON response below in SOAP UI:- I need to find number of JSON Nodes in this JSON. I tried with JSONPATH Count but it doesn't give accurate result. Here 8 nodes are there wich is expected as output.
<data contentType="text/plain" contentLength="772"><![CDATA[[{
"EmployeeId": "99",
"EmployeeName": "Doe",
"Role": "Dr"
},
{
"EmployeeId": "88",
"EmployeeName": "John",
"Role": "Dr"
},
{
"EmployeeId": "999",
"EmployeeName": "Doe",
"Role": "Dr"
},
{
"EmployeeId": "888",
"EmployeeName": "John",
"Role": "Dr"
},
{
"EmployeeId": "777",
"EmployeeName": "Keerthi",
"Role": "Dr"
},
{
"EmployeeId": "666",
"EmployeeName": "Keerthi",
"Role": "Dr"
},
{
"EmployeeId": "1234",
"EmployeeName": "Sushant",
"Role": "Doctor"
},
{
"EmployeeId": "107",
"EmployeeName": "John8",
"Role": "LabTech"
}]]]></data>
如何查找节点数?
推荐答案
您可以提取列表并使用size()
方法对列表进行计数,并使用groovy script
来获取计数.
You may extract the list and use size()
method on it get the count using groovy script
.
例如:
def employees = '''
[
{
"EmployeeId": "88",
"EmployeeName": "John",
"Role": "Dr"
},
{
"EmployeeId": "999",
"EmployeeName": "Doe",
"Role": "Dr"
}
]'''
def employeeList = new groovy.json.JsonSlurper().parseText( employees )
println employeeList.size()
输出是预期的2.
更新:
如果要提供所提供的特定数据,请继续.
由于数据不是直接json类型,即包装在xml的cdata
中.
因此,首先需要从xml中提取json数据,然后应用大小来获取计数.
Since the data is not directly json type, i.e., wrapped in cdata
which is part of xml.
So first need to extract json data from xml then apply size to get the count.
def xml = '''
<data contentType="text/plain" contentLength="772"><![CDATA[[{
"EmployeeId": "99",
"EmployeeName": "Doe",
"Role": "Dr"
},
{
"EmployeeId": "88",
"EmployeeName": "John",
"Role": "Dr"
},
{
"EmployeeId": "999",
"EmployeeName": "Doe",
"Role": "Dr"
},
{
"EmployeeId": "888",
"EmployeeName": "John",
"Role": "Dr"
},
{
"EmployeeId": "777",
"EmployeeName": "Keerthi",
"Role": "Dr"
},
{
"EmployeeId": "666",
"EmployeeName": "Keerthi",
"Role": "Dr"
},
{
"EmployeeId": "1234",
"EmployeeName": "Sushant",
"Role": "Doctor"
},
{
"EmployeeId": "107",
"EmployeeName": "John8",
"Role": "LabTech"
}]]]></data>'''
def data = new XmlSlurper().parseText(xml)
def list = new groovy.json.JsonSlurper().parseText( data.toString())
log.info list.size()
您也可以使用:
对其进行断言.
assert 8 == list.size(), "Employee count is not matching"
You may also assert it using :
assert 8 == list.size(), "Employee count is not matching"
这篇关于SOAPUI:如何在JSON响应中查找节点数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!