• <bdo id='wOmLY'></bdo><ul id='wOmLY'></ul>

    <legend id='wOmLY'><style id='wOmLY'><dir id='wOmLY'><q id='wOmLY'></q></dir></style></legend>
      <i id='wOmLY'><tr id='wOmLY'><dt id='wOmLY'><q id='wOmLY'><span id='wOmLY'><b id='wOmLY'><form id='wOmLY'><ins id='wOmLY'></ins><ul id='wOmLY'></ul><sub id='wOmLY'></sub></form><legend id='wOmLY'></legend><bdo id='wOmLY'><pre id='wOmLY'><center id='wOmLY'></center></pre></bdo></b><th id='wOmLY'></th></span></q></dt></tr></i><div id='wOmLY'><tfoot id='wOmLY'></tfoot><dl id='wOmLY'><fieldset id='wOmLY'></fieldset></dl></div>
    1. <small id='wOmLY'></small><noframes id='wOmLY'>

        <tfoot id='wOmLY'></tfoot>

        Postman - 如何计算 JSON 响应中特定对象的出现次数

        时间:2023-10-20
          <bdo id='s8KzX'></bdo><ul id='s8KzX'></ul>
          <legend id='s8KzX'><style id='s8KzX'><dir id='s8KzX'><q id='s8KzX'></q></dir></style></legend>

          <small id='s8KzX'></small><noframes id='s8KzX'>

            <tfoot id='s8KzX'></tfoot>
                <tbody id='s8KzX'></tbody>
            • <i id='s8KzX'><tr id='s8KzX'><dt id='s8KzX'><q id='s8KzX'><span id='s8KzX'><b id='s8KzX'><form id='s8KzX'><ins id='s8KzX'></ins><ul id='s8KzX'></ul><sub id='s8KzX'></sub></form><legend id='s8KzX'></legend><bdo id='s8KzX'><pre id='s8KzX'><center id='s8KzX'></center></pre></bdo></b><th id='s8KzX'></th></span></q></dt></tr></i><div id='s8KzX'><tfoot id='s8KzX'></tfoot><dl id='s8KzX'><fieldset id='s8KzX'></fieldset></dl></div>
                  本文介绍了Postman - 如何计算 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"属性的 count

                  In the example below I want to get the count of All "IsArchived" attributes in the response;

                  这些属性的数量因响应而异.

                  The number of those attributes will vary from response to response.

                  我该怎么做?提前致谢

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

                  推荐答案

                  这是一个相当具体的解决方案,但我希望它有所帮助.描述被添加为评论:

                  It is a rather specific solution but I hope it helps. The description is added as comments:

                  // Convert the response body to a JSON object
                  var jsonData = pm.response.json()
                  
                  // Create a count variable which will be increased by 1 everytime IsArchived occurs
                  var count = 0;
                  
                  function countIsArchived() {
                      // Loop through the FieldGroupsArray
                      _.each(jsonData.FieldGroups, (fieldGroupsArray) => {
                          // Loop through the FieldDefinitionsArray
                          _.each(fieldGroupsArray.FieldDefinitions, (fieldDefinitionsArray) => {
                              // Check if IsArchived exists
                              if(fieldDefinitionsArray.IsArchived) {
                                  // Increase count by 1
                                  count++;
                              }
                          });
                      });
                  
                      // IF you want it:
                      // Check if IsArchived exists on the top level of the JSON response and increase count
                      if(jsonData.IsArchived) {
                          count++;
                      }
                      // IF you want it:
                      // Create a Postman environment variable and assign the value of count to it
                      pm.environment.set("count", count);
                  }
                  

                  附加信息:

                  不需要后面的,.它使 JSON 无效:

                  The , after the following object is not needed. It invalidates the JSON:

                  {
                      "Id": 33,
                      "DisplayName": "Long Description",
                      "IsArchived": false
                  },  <--
                  

                  这篇关于Postman - 如何计算 JSON 响应中特定对象的出现次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何在 CI 环境中运行 postman 的 newman? 下一篇:我们可以从邮递员中的 excel 中读取数据进行 js 测试吗?

                  相关文章

                    <bdo id='242kb'></bdo><ul id='242kb'></ul>
                1. <legend id='242kb'><style id='242kb'><dir id='242kb'><q id='242kb'></q></dir></style></legend>
                  1. <small id='242kb'></small><noframes id='242kb'>

                    <i id='242kb'><tr id='242kb'><dt id='242kb'><q id='242kb'><span id='242kb'><b id='242kb'><form id='242kb'><ins id='242kb'></ins><ul id='242kb'></ul><sub id='242kb'></sub></form><legend id='242kb'></legend><bdo id='242kb'><pre id='242kb'><center id='242kb'></center></pre></bdo></b><th id='242kb'></th></span></q></dt></tr></i><div id='242kb'><tfoot id='242kb'></tfoot><dl id='242kb'><fieldset id='242kb'></fieldset></dl></div>

                    1. <tfoot id='242kb'></tfoot>