• <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 响应中特定对象的出现次数

        Postman - How to count occurrences of a specific object in a JSON response(Postman - 如何计算 JSON 响应中特定对象的出现次数)
          <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 响应中特定对象的出现次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Toggle HTML radio button by clicking its label(通过单击标签来切换 HTML 单选按钮)
                  Javascript how to change radio button label text?(Javascript如何更改单选按钮标签文本?)
                  JavaScript radio button confirmation(JavaScript 单选按钮确认)
                  How can I automatically select specific radio buttons with Greasemonkey?(如何使用 Greasemonkey 自动选择特定的单选按钮?)
                  AngularJs. Is it possible to deselect HTML “radio” input by click?(AngularJs.是否可以通过单击取消选择 HTML“收音机输入?)
                  Checking Value of Radio Button Group via JavaScript?(通过 JavaScript 检查单选按钮组的值?)
                    <bdo id='242kb'></bdo><ul id='242kb'></ul>
                    • <legend id='242kb'><style id='242kb'><dir id='242kb'><q id='242kb'></q></dir></style></legend>
                          <tbody id='242kb'></tbody>

                          <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>