邮递员-如何从JSON响应中获取最后一次出现的必需属性的值

时间:2019-04-01 22:41:32

标签: json postman

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

我创建了一个GET请求,该请求将获得如下所示的JSON响应。

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

我该怎么办? (尽管也许可以通过回答我的其他问题来回答)预先感谢

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

2 个答案:

答案 0 :(得分:0)

这应该很容易,因为您的IsArchived属性位于JSON响应的顶层,因此除非您以某种方式遍历{{1 }}数组,就像您在其他问题中一样。

IsArchived

答案 1 :(得分:0)

首先,您要处理的JSON无效。您始终可以使用https://babeljs.io/repl检查JSON有效性。

这是有效的JSON:

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

现在,我假设您想在Postman测试中获取属性,可以按以下步骤实现:

var responseData = pm.response.json();
var isArchived = responseData.isArchived;