如何在逻辑应用中循环遍历数组?

4
我已经成功地将所有的用户数据存储在一个数组中(请见这里),但现在我无法循环遍历数据。在构建了数组后,我已将其转换为JSON,但我不能再按照JSON模式中定义的字段进行访问。
在循环遍历中,我只能访问JSON主体而不是像用户名、邮箱等单独字段。
我应该改变我的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的格式:

JSON Schema

1个回答

8
根据我的理解,您只想循环数组以获取每个项目的名称、邮件和其他一些字段。如您在问题中提到的,您可以使用json主体作为ForEach循环的输入。没问题,不需要做任何其他操作。请参考下面的屏幕截图:
  1. 像你的json数据一样初始化变量。 在此输入图片描述

  2. 然后通过“解析JSON”操作对其进行解析。 在此输入图片描述

  3. 现在,将正文设置为For each循环的输入,然后使用变量并从“解析JSON”中设置值为“mail”。 在此输入图片描述

  4. 运行逻辑应用程序后,我们可以看到邮件字段也被循环。您可以在“ForEach”中轻松使用“邮件”,“名称”和其他字段。 在此输入图片描述 在此输入图片描述

更新:

我已经检查了您的JSON模式,但似乎无法匹配您在屏幕截图中提供的JSON数据。请问您是如何生成JSON模式的?在我的端上,我只需点击“使用示例负载生成模式”按钮,它就会自动生成模式。 enter image description here

我使用与您相同结构的JSON数据样本并生成其模式,请参考以下JSON数据和模式:

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模式有问题。 我已经在我的原始问题中粘贴了整个模式。 - Martijn Balink
嗨@MartijnBalink,看起来你的JSON模式与你的JSON数据不匹配。我已经更新了我的答案,请检查一下。如果还有任何问题,请随时让我知道~ - Hury Shen
嗨@MartijnBalink,不知道你的问题解决了吗? - Hury Shen
嗨 @HuryShen,你能帮我解答一下问题吗? - Lynn
1
太棒了,最好的教程。第一遍就完美地运作了,谢谢! - spadelives

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接