如何从邮递员中的JSONArray获取JSONobject [英] How to get JSONobject from JSONArray in postman

查看:103
本文介绍了如何从邮递员中的JSONArray获取JSONobject的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用测试脚本
自动化邮递员中的注册方案,我有以下JsonArray作为响应:

I am trying to automize a registration scenario in postman using test scripts I have the following JsonArray as a response:

[
    {
        "id": 1,
        "name": "user_A",
        "cntkp": "martin",
        "company": "kreativ",
        "tel": "12345678",
        "email": "user_A@gmail.com"
        "street": "str. 0001",
        "city": "DEF",
    }
    ......
    ......
    ......
    {
        "id": 4,
        "name": "user_B",
        "cntkp": "martin",
        "company": "kreativ",
        "tel": "12345678",
        "email": "user_B@gmail.com"
        "street": "str. 0002",
        "city": "NJ",
    }
    ......
    ......
    ......
    {
        "id": 10,
        "name": "User_C",
        "cntkp": "martin",
        "company": "kreativ",
        "tel": "12345678",
        "email": "user_C@gmail.com"
        "street": "str. 0003",
        "city": "ABC",
    }
    ......
]

数组长度可以动态更改(在此示例中为10),并希望使用特殊电子邮件(数组中的某个位置)查找对象,然后从中获取ID

the array length can be dynamic and changed (in this sample is 10) and want to find the object with special email (somewhere in the array) and then get the ID from that object and make the assertion based on JsonData from this object (catch elements e.g. check name).

我该怎么做?

感谢支持。

我发送GETrequest以从Registration DB中获取所有数据。
作为响应,我得到了JsonArray $ b来自Json Array的$ b我需要用于断言的特定对象(例如,示例中带有电子邮件user_B的对象)。
我知道我的电子邮件地址并以此为基础,我必须从Object中找到ID。
我可以在知道哪个ID是我的ID的情况下执行此操作,但如果它是动态的,我不知道如何在邮递员中搜索数组以获取ID

I send a GETrequest to get all Data from Registration DB. as response I get a JsonArray from Json Array I need the specific Object for the assertion (e.g. object with email user_B in sample) . I know my Email address and base on it I have to findout the ID from Object . I can do it when I know which ID is my ID but in case it is dynamic I don't know how to search an array for it in postman to get ID

例如,要声明公司名称

pm.expect(jsonData[0].company).to.equal(pm.environment.get("regDB_new_company"))

不知道ID(仅知道我的电子邮件),我必须先找出对象的ID,然后才能对其进行评估。

but if I dont know the ID ( only know my email) I have first to find out the ID of Object then I can asser it.

例如
在这种情况下,首先找到带有电子邮件 user_B@gmail.com的对象
,然后从该对象中获取ID元素(在这种情况下为4)
,然后我想对所有数据进行断言从对象

e.g. in this case first, find the object with email "user_B@gmail.com" then from that object get ID element (in this case 4) then I want to assert for all data from the object

推荐答案

感谢丹尼,我找到了解决方法

Thanks Danny, I found the solution

var arr = pm.response.json()

for(i = 0; i < arr.length; i++) {
    if (arr[i].email == "userB@gmail.com") {
        pm.environment.set("personID", arr[i].id)
    }
}

这篇关于如何从邮递员中的JSONArray获取JSONobject的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆