如何使用SOUPUI从Json响应中获取特定值

时间:2016-07-13 03:06:25

标签: json web-api-testing

我是SOUPUI的新手,我能够阅读restapi的响应并能够保存到文件,我在这个地方徘徊,我想从响应中存储一个值。请帮忙。

我的问题:

  1. 如何从以下响应中存储随机employeeId。(随机选择任何employeeId)?
  2. 如何使用员工姓名获得特定的employeeId(如我想得到" employeeId"如果" firstName"名称是" Daitha")?
  3. 如何使用数组读取employeeId(如第一个,第二个..employeeId)?
  4. 以下是样本回复:

    {
        "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"
                }]
            }
        }
    }
    

    先谢谢

2 个答案:

答案 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 
}