我是SOUPUI的新手,我能够阅读restapi的响应并能够保存到文件,我在这个地方徘徊,我想从响应中存储一个值。请帮忙。
我的问题:
以下是样本回复:
{
"response": {
"stat": "SUCCESS",
"result": {
"employees": [{
"employeeId": "7d58129a-5ca3-4acd-a601-11478ba47988",
"firstName": "Daitha",
"lastName": "shankar",
"loginName": "xxxxx@xxx.com",
"mobileNumber": "xxxxxxxxxxxx",
"emailId": "xxxxxxx@xxxx.com",
"rowStatus": "A",
"assetCount": 3087,
"canUseapp": "Y",
"supportUserInd": "N"
}, {
"employeeId": "e2dec6de-8882-4c5b-a875-41fffe8e977f",
"firstName": "john",
"lastName": "deo",
"loginName": "xxxx@yyy.com",
"emailId": "xxxx@xxx.com",
"rowStatus": "A",
"assetCount": 0,
"canUseapp": "Y",
"supportUserInd": "N"
}, {
"employeeId": "9a9e7ff6-edb7-402a-bed2-27e9036a716f",
"tenantBadgeId": "EMP11659824",
"firstName": "suman",
"lastName": "m",
"loginName": "eeee@sfsdf.om",
"mobileNumber": "xxxxxx",
"emailId": "xxxxxx@xxxxxx.com",
"rowStatus": "A",
"designation": "software QA engineer",
"assetCount": 0,
"canUseapp": "Y",
"supportUserInd": "N"
}, {
"employeeId": "9ecf7fc8-c06d-4e3c-a3a3-d2c50509c16b",
"firstName": "vinay",
"lastName": "B",
"loginName": "xxxxxx@.dsfsd.com",
"emailId": "xxxxx.@xxxx.com",
"rowStatus": "A",
"assetCount": 0,
"canUseapp": "Y",
"supportUserInd": "N"
}]
}
}
}
先谢谢
答案 0 :(得分:1)
<!DOCTYPE html>
<html>
<body>
<p>How to create a JavaScript object array.</p>
<p id="demo"></p>
<script>
var employees = '[{"employeeId": "7d58129a-5ca3-4acd-a601-11478ba47988","firstName": "Daitha","lastName": "shankar","loginName": "xxxxx@xxx.com","mobileNumber": "xxxxxxxxxxxx","emailId": "xxxxxxx@xxxx.com","rowStatus": "A","assetCount": 3087,"canUseapp": "Y","supportUserInd": "N"}, {"employeeId": "e2dec6de-8882-4c5b-a875-41fffe8e977f","firstName": "john","lastName": "deo","loginName": "xxxx@yyy.com","emailId": "xxxx@xxx.com","rowStatus": "A","assetCount": 0,"canUseapp": "Y","supportUserInd": "N"}, {"employeeId": "9a9e7ff6-edb7-402a-bed2-27e9036a716f","tenantBadgeId": "EMP11659824","firstName": "suman","lastName": "m","loginName": "eeee@sfsdf.om","mobileNumber": "xxxxxx","emailId": "xxxxxx@xxxxxx.com","rowStatus": "A","designation": "software QA engineer","assetCount": 0,"canUseapp": "Y","supportUserInd": "N"}, {"employeeId": "9ecf7fc8-c06d-4e3c-a3a3-d2c50509c16b","firstName": "vinay","lastName": "B","loginName": "xxxxxx@.dsfsd.com","emailId": "xxxxx.@xxxx.com","rowStatus": "A","assetCount": 0,"canUseapp": "Y","supportUserInd": "N"}]';
var arr = JSON.parse(employees);
var i;
var out = "<table>";
for(i = 0; i < arr.length; i++) {
if(arr[i].firstName=="Daitha")
{
out += "<tr><td>" +
arr[i].employeeId +
"</td><td>" +
arr[i].firstName +
"</td></tr>";
}
}
out += "</table>";
document.getElementById("demo").innerHTML = out;
</script>
</body>
</html>
答案 1 :(得分:1)
Shankar,第
你可以玩下面的groovy脚本
import groovy.json.JsonSlurper
def responseContent = testRunner.testCase.getTestStepByName("Emp").getPropertyValue("response")
def r = new JsonSlurper().parseText(responseContent)
def employeesSize = r.response.result.employees.size()
//log.info "employee size:"+employeesSize
def list=new Object[4]
for(int i=0;i<employeesSize;i++)
{
//log.info r.response.result.employees[i].firstName
list[i] = r.response.result.employees[i].lastName
log.info list[i]
}
//log.info(list[3])
//log.info "stat:"+r.response.stat
for(int i=0;i<list.size();i++)
{
//list.each{log.info(it)}
def p=list[i]
if(p=="vinay")
{
//log.info "yes"
log.info r.response.result.employees[i].employeeId
}
else
{
//log.info "no"
}
}
//log.info r.response.result.employees[0].size()
def listempdata = r.response.result.employees[0]
//log.info listempdata
def k =new Object[r.response.result.employees[0].size()]
for(int j=1;j<r.response.result.employees[0].size();j++){
//k[j]=listempdata.each{it}.toString().split(",")
//listempdata.each{log.info (it)}
//log.info listempdata.lastName
}