【问题标题】:How to loop through an array in a logic app?如何在逻辑应用程序中循环遍历数组?
【发布时间】:2020-04-06 23:49:51
【问题描述】:

我已经设法将我的所有用户数据放在一个数组中(请参阅here),但现在我无法遍历数据。构建数组后,我已将其转换为 JSON,但我无法再处理我的 JSON 架构中定义的字段。

我在循环中唯一可以解决的问题(我使用 JSON 正文作为 For Each 循环的输入)是正文本身,而不是单独的字段,如用户名、邮件地址等。

我应该更改我的 JSON 架构中的某些内容来克服这个问题还是有其他问题?

编辑:请在下面找到我的 JSON 架构:

   {
       "$schema": "http://json-schema.org/draft-04/schema#",
       "items": [
           {
               "properties": {
                   "@@odata.type": {
                       "type": "string"
                   },
                   "createdDateTime": {
                       "type": "string"
                   },
                   "employeeId": {
                       "type": "string"
                   },
                   "givenName": {
                       "type": "string"
                   },
                   "id": {
                       "type": "string"
                   },
                   "mail": {
                       "type": "string"
                   },
                   "onPremisesSamAccountName": {
                       "type": "string"
                   },
                   "surname": {
                       "type": "string"
                   },
                   "userPrincipalName": {
                       "type": "string"
                   }
               },
               "required": [
                   "@@odata.type",
                   "id",
                   "givenName",
                   "surname",
                   "userPrincipalName",
                   "mail",
                   "onPremisesSamAccountName",
                   "employeeId",
                   "createdDateTime"
               ],
               "type": "object"
           }
       ],
       "type": "array"
   }

请查看图片了解 JSON 的外观:

【问题讨论】:

    标签: arrays json azure-logic-apps power-automate


    【解决方案1】:

    根据我的理解,您只想循环数组以获取每个项目的名称、邮件和其他一些字段。正如您在问题中提到的,您可以使用 json 正文作为 For Each 循环的输入。没关系,没有必要再做任何事情了。请参考以下截图:

    1. 初始化一个变量,比如你的 json 数据。

    2. 然后通过“解析 JSON”动作解析它。

    3. 现在,将正文设置为 For each 循环的输入,然后使用变量并使用“解析 JSON”中的“邮件”设置值。

    4. 运行逻辑应用后,我们可以看到邮件字段也被循环了。您可以在“For each”中轻松使用“邮件”、“姓名”等字段。

    更新:

    我检查了您的 json 架构,但它似乎与您在屏幕截图中提供的 json 数据不匹配。请问您是如何生成 json 架构的,在我这边,我只需单击“使用示例有效负载生成架构”按钮即可生成 json 架构,它会自动生成架构。

    我使用了一个和你的结构相同的json数据样本并生成了它的schema,请参考下面的json数据和schema:

    json 数据:

    {
        "body": [
            {
                "@odata.type": "test",
                "id": "123456",
                "givenName": "test",
                "username": "test",
                "userPrincipalName": "test",
                "mail": "test@mail.com",
                "onPremisesSamAccountName": "test",
                "employeeId": "test",
                "createdDateTime": "testdate"
            },
            {
                "@odata.type": "test",
                "id": "123456",
                "givenName": "test",
                "username": "test",
                "userPrincipalName": "test",
                "mail": "test@mail.com",
                "onPremisesSamAccountName": "test",
                "employeeId": "test",
                "createdDateTime": "testdate"
            }
        ]
    }
    

    架构:

    {
        "type": "object",
        "properties": {
            "body": {
                "type": "array",
                "items": {
                    "type": "object",
                    "properties": {
                        "@@odata.type": {
                            "type": "string"
                        },
                        "id": {
                            "type": "string"
                        },
                        "givenName": {
                            "type": "string"
                        },
                        "username": {
                            "type": "string"
                        },
                        "userPrincipalName": {
                            "type": "string"
                        },
                        "mail": {
                            "type": "string"
                        },
                        "onPremisesSamAccountName": {
                            "type": "string"
                        },
                        "employeeId": {
                            "type": "string"
                        },
                        "createdDateTime": {
                            "type": "string"
                        }
                    },
                    "required": [
                        "@@odata.type",
                        "id",
                        "givenName",
                        "username",
                        "userPrincipalName",
                        "mail",
                        "onPremisesSamAccountName",
                        "employeeId",
                        "createdDateTime"
                    ]
                }
            }
        }
    }
    

    【讨论】:

    • 您好,感谢您的回复!这一切对我来说似乎都很清楚(这不是我构建的 JSON 集合的第一个循环),但由于某种原因,这个循环不起作用。我认为 JSON 模式有问题。我已在原始问题中粘贴了整个架构。
    • 嗨@MartijnBalink,您的 json 架构似乎与您的 json 数据不匹配。我已经更新了我的答案,请检查它。如果还有什么问题,请随时告诉我~
    • 嗨@MartijnBalink,请问您的问题是否已解决?
    • 嗨@HuryShen,你能帮我解答一下吗?
    • 太棒了,有史以来最好的教程。第一次完美运行,谢谢!
    猜你喜欢
    • 1970-01-01
    • 2012-12-30
    • 2023-02-16
    • 2013-02-01
    • 2020-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多