简体   繁体   中英

How to get the elements from the json array in postman?

This code finds the first element:

var jsonObject = xml2Json(responseBody);
pm.collectionVariables.set("VehicleVisit",jsonObject['soap:Envelope']['soap:Body'].findVehicleVisitsResponse.findVehicleVisitsResult.allVehicleVisits.WS_VehicleVisit[0].visitNumber);

and this code finds the last element:

var jsonObject = xml2Json(responseBody),
    vehicleVisitArray = jsonObject['soap:Envelope']['soap:Body'].findVehicleVisitsResponse.findVehicleVisitsResult.allVehicleVisits.WS_VehicleVisit;

pm.collectionVariables.set("VehicleVisit", vehicleVisitArray[vehicleVisitArray.length - 1].visitNumber);

I want something to find in both ways, I have tried by using if condition in for loop with the following code but it didn't work, can someone help?

var jsonObject = xml2Json(responseBody),
    vehicleVisitArray = jsonObject['soap:Envelope']['soap:Body'].findVehicleVisitsResponse.findVehicleVisitsResult.allVehicleVisits.WS_VehicleVisit;
    
    

    for(i = 0; i < vehicleVisitArray.length; i++)
    {
        if(vehicleVisitArray[i].visitNumber == vehicleVisitArray.visitNumber)
        {
            pm.collectionVariables.set("VehicleVisit", vehicleVisitArray[i].visitNumber);
        }
        else
        {
            pm.collectionVariables.set("VehicleVisit", vehicleVisitArray[vehicleVisitArray.length - 1].visitNumber);
        }
    }
var jsonObject = xml2Json(responseBody),
    vehicleVisitArray = jsonObject['soap:Envelope']['soap:Body'].findVehicleVisitsResponse.findVehicleVisitsResult.allVehicleVisits.WS_VehicleVisit;
    
    

    for(i = 0; i < vehicleVisitArray.length; i++)
    {
        if(vehicleVisitArray[i].visitNumber == vehicleVisitArray[0].visitNumber)
        {
            pm.collectionVariables.set("VehicleVisit", vehicleVisitArray[i].visitNumber);
        }
        else
        {
            pm.collectionVariables.set("VehicleVisit", vehicleVisitArray[vehicleVisitArray.length - 1].visitNumber);
        }
    }

you should add index as vehicleVisitArray is an array and

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM