我在SOAP UI中有以下JSON响应: - 我需要在这个JSON中找到许多JSON节点。 我试过JSONPATH Count,但它没有给出准确的结果。 这里有8个节点作为输出。
<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>
如何查找节点计数?
答案 0 :(得分:0)
您可以提取列表并使用size()
方法使用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类型,即包含在cdata
中,它是xml的一部分。
因此,首先需要从xml中提取json数据,然后应用size来获取计数。
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"