邮递员-如何从JSON响应中获取最后一次出现的必需属性的值 [英] Postman - How to get the value from a JSON response for the last occurrence of the required attributes

查看:79
本文介绍了邮递员-如何从JSON响应中获取最后一次出现的必需属性的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是JSON和Postman的新手。我相信我正在尝试做一些非常简单的事情。

I am new to JSON and Postman. I believe I'm trying to do something very simple.

我创建了一个GET请求,该请求将获得类似于以下内容的JSON响应。

I have created a GET request which will get a JSON response like the one below.

在下面的示例中,我想获取最后一个 IsArchived属性( Version之前的属性)的值;或者不属于FieldGroup的人。

In the example below I want to get the value of the last "IsArchived" attribute (the one just before the "Version"); Or the one that does not belong to a FieldGroup.

我该怎么办? (尽管也许可以通过回答我的其他问题得到解答)

How can I do it? (although maybe it will be answered by an answer to my other question) Thanks in advance

{
    "Id": 1328,
    "Name": "AAA Test",
    "Owner": {
        "Id": 208,
        "Name": "The Boss"
    },
    "FieldGroups": [
        {
            "Id": "c81376f0-6ac3-4028-8d61-76a0f815dbf8",
            "Name": "General",
            "FieldDefinitions": [
                {
                    "Id": 1,
                    "DisplayName": "Product Name",
                    "IsArchived": false
                },
                {
                    "Id": 2,
                    "DisplayName": "Short Description",
                    "IsArchived": false
                },
                {
                    "Id": 33,
                    "DisplayName": "Long Description",
                    "IsArchived": false
                },
            ]
        },
        {
            "Id": "5ed8746b-0fa8-4022-8216-ad3af17db91f",
            "Name": "Somethingelse",
            "FieldDefinitions": [
                {
                    "Id": 123,
                     "DisplayName": "Attribution",
                    "IsArchived": false
                },
                {
                    "Id": 1584,
                    "DisplayName": "FC1",
                    "IsArchived": false
                },
                {
                    "Id": 623,
                    "DisplayName": "Sizes",
                    "IsArchived": false,
                    "Owner": {
                        "Id": 208,
                        "Name": "The Boss"
                    },
                    "Unit": "",
                    "Options": [
                        {
                            "Id": 1,
                            "Value": "XS"
                        },
                        {
                            "Id": 2,
                            "Value": "S"
                        },
                        {
                            "Id": 3,
                            "Value": "M"
                        },
                    ]
                }
             ]
        }
    ],
    },
    "IsArchived": false
    "Version": 1
}


推荐答案

这应该很容易,因为您的 IsArchived 属性位于JSON响应的最高级别,因此除非您以某种方式遍历 FieldGroups IsArchived 值>就像您在其他问题中一样。

This should be rather easy because your IsArchived attribute is on the top level of the JSON response, so it will not happen that another IsArchived value is used unless you somehow loop through your FieldGroups array like in your other question.

// Convert the response body to a JSON object
var jsonData = pm.response.json()

// Create a variable and assign the value of IsArchived to it
var isArchived = jsonData.IsArchived;

// OR create a Postman environment variable and assign the value of IsArchived to it
pm.environment.set("isArchived", jsonData.IsArchived);

这篇关于邮递员-如何从JSON响应中获取最后一次出现的必需属性的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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