arrays - 如何循环遍历逻辑应用程序中的数组?

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

我已经设法将我所有的用户数据放在一个数组中(请参阅 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 的外观:

JSON Schema

最佳答案

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

  1. 像 json 数据一样初始化一个变量。 enter image description here

  2. 然后通过“解析 JSON”操作对其进行解析。 enter image description here

  3. 现在,将正文设置为 For each 循环的输入,然后使用变量并使用“Parse JSON”中的“mail”设置值。 enter image description here

  4. 运行逻辑应用后,我们可以看到邮件字段也是循环的。您可以在“For each”中轻松使用“邮件”、“姓名”和其他字段。 enter image description here enter image description here

更新:

我检查了您的 json 架构,但它似乎与您在屏幕截图中提供的 json 数据不匹配。我可以知道你是如何生成你的 json 模式的吗,在我这边,我只是通过单击“使用示例有效负载生成模式”按钮来生成 json 模式,它会自动生成模式。 enter image description here

我使用了一个与你的结构相同的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"
                ]
            }
        }
    }
}

关于arrays - 如何循环遍历逻辑应用程序中的数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59324036/

相关文章:

javascript - 将嵌套数组索引与 JavaScript 映射求和

c++ - 动态创建的数组大小不匹配

php - 引用 - 这个错误在 PHP 中意味着什么?

javascript - 如何在 Javascript 中格式化 JSON 日期?

Azure 逻辑应用程序 - 如何将文件从字节数组上传到 Azure Blob 存储

Javascript - 基于动态填充下拉菜单的转到 url

python - 如何 json 序列化自定义可迭代对象?

python - RDF/XML 格式转 JSON

workflow - 跳过逻辑应用连接器

azure-active-directory - 从 Azure 逻辑应用程序访问 AD 用户时出现错误 403