【问题标题】:extract contacts from account using High order function使用高阶函数从帐户中提取联系人
【发布时间】:2021-02-03 15:47:37
【问题描述】:

如何使用高阶函数从账户 JSON 对象中提取联系人和账户显示名称列表。我需要的是在一个数组中提取并展平联系人名字和姓氏

我的快速对象

  struct Account: Codable {
        var accountID, displayName, managedByStakeholderID, id: String?
        var contacts: [Contact]?
    }
struct Contact: Codable {

    var firstName: String?
    var lastName: String?
}

JSON

    {
       "Accounts":[
          {
             "AccountID":"ef391c24-ad5b-4dca-98ae-b55793dfc029",
             "Contacts":[
                {
                   "AccountID":"ef391c24-ad5b-4dca-98ae-b55793dfc029",
                   "AvatarImage":"",
                   "ContactEmail":"cbacon@associates.com",
                   "ContactID":"f74d3810-625f-43d4-afd4-89346a73f807",
                   "ContactTitle":"Chief Executive Officer",
                   "DisplayName":"Ch  Bacon",
                   "FirstName":"Ch",
                   "LastName":"Baco",
                   "ManagedByStakeholderID":"97e5f0d5-df0d-48fe-bbbb-cccf251f4175",
                   "Notes":"OWNER",
                   "Roles":[
                      {
                         "RoleEnvironmentName":"",
                         "RoleEnvironmentTenantName":"",
                         "RoleID":"3a824b1e-26d6-41c5-8336-4b79f0962c38",
                         "RoleName":"Environment Administrator"
                      }
                   ],
                   "StakeholderID":"97e5f0d5-df0d-48fe-bbbb-cccf251f4175",
                   "id":"f74d3810-625f-43d4-afd4-89346a73f807"
                }
             ],
             "DisplayName":"Chris Associates Ltd."
          },
          {
             "AccountID":"C31ACAD6-0E5D-43E0-B1A2-E51FD46BFE3A",
             "Contacts":[
                {
                   "AccountID":"C31ACAD6-0E5D-43E0-B1A2-E51FD46BFE3A",
                   "AvatarImage":"http://www.image.com/",
                   "ContactEmail":"jhgjhg@njkjk.com",
                   "ContactID":"28fad61f-9a47-41db-888e-19d66de96638",
                   "ContactTitle":"",
                   "DisplayName":"fvdfvdf",
                   "FirstName":"dfvdfv",
                   "LastName":"dfvdfv",
                   "ManagedByStakeholderID":"97e5f0d5-df0d-48fe-bbbb-cccf251f4175",
                   "Notes":"",
                   "Roles":[
                      
                   ],
                   "StakeholderID":"43d3af87-fd21-404c-9af6-2998e401fcb8",
                   "id":"28fad61f-9a47-41db-888e-19d66de96638"
                }
             ],
             "DisplayName":"adidas "
          },
          {
             "AccountID":"A4FBDC11-AB07-47BD-8364-0B1D81D8E38F",
             "Contacts":[
                {
                   "AccountID":"A4FBDC11-AB07-47BD-8364-0B1D81D8E38F",
                   "AvatarImage":"http://www.image.com/",
                   "ContactEmail":"jeff@linkedin.com",
                   "ContactID":"c205d05d-c4cf-46b2-a9fd-e85af2f317b9",
                   "ContactTitle":"",
                   "DisplayName":"Jeff Weiner",
                   "FirstName":"Jeff",
                   "LastName":"Weiner",
                   "ManagedByStakeholderID":"97e5f0d5-df0d-48fe-bbbb-cccf251f4175",
                   "Notes":"",
                   "Roles":[
                      
                   ],
                   "StakeholderID":"c6fc9b6e-f857-4ca1-8352-dc55b05a9373",
                   "id":"c205d05d-c4cf-46b2-a9fd-e85af2f317b9"
                }
             ],
             "DisplayName":"LinkedIn. Inc "
          }
       ]
    }

【问题讨论】:

  • 这取决于您希望如何提取它们(全部或过滤)以及您期望的输出类型。
  • 我需要的是联系人名字和姓氏提取并展平在一个数组中
  • 将该信息添加到问题中,并包括联系人的定义

标签: json swift filter


【解决方案1】:

您可以获取您的响应帐户属性并 flatMap 联系人:

struct Response: Decodable {
    let accounts: [Account]
    enum CodingKeys: String, CodingKey {
        case accounts = "Accounts"
    }
}

struct Account: Decodable {
    let accountID, displayName, managedByStakeholderID, id: String?
    let contacts: [Contact]
    enum CodingKeys: String, CodingKey {
        case accountID = "AccountID", displayName = "DisplayName", managedByStakeholderID = "ManagedByStakeholderID", id = "ID", contacts = "Contacts"
    }
}

struct Contact: Decodable {
    let firstName: String
    let lastName: String
    enum CodingKeys: String, CodingKey {
        case firstName = "FirstName", lastName = "LastName"
    }
}

let jsonResponse = """
{
      "Accounts":[
         {
            "AccountID":"ef391c24-ad5b-4dca-98ae-b55793dfc029",
            "Contacts":[
               {
                  "AccountID":"ef391c24-ad5b-4dca-98ae-b55793dfc029",
                  "AvatarImage":"",
                  "ContactEmail":"cbacon@cpb&associates.com",
                  "ContactID":"f74d3810-625f-43d4-afd4-89346a73f807",
                  "ContactTitle":"Chief Executive Officer",
                  "DisplayName":"Ch  Bacon",
                  "FirstName":"Ch",
                  "LastName":"Baco",
                  "ManagedByStakeholderID":"97e5f0d5-df0d-48fe-bbbb-cccf251f4175",
                  "Notes":"OWNER",
                  "Roles":[
                     {
                        "RoleEnvironmentName":"",
                        "RoleEnvironmentTenantName":"",
                        "RoleID":"3a824b1e-26d6-41c5-8336-4b79f0962c38",
                        "RoleName":"Environment Administrator"
                     }
                  ],
                  "StakeholderID":"97e5f0d5-df0d-48fe-bbbb-cccf251f4175",
                  "id":"f74d3810-625f-43d4-afd4-89346a73f807"
               }
            ],
            "DisplayName":"Chris Associates Ltd."
         },
         {
            "AccountID":"C31ACAD6-0E5D-43E0-B1A2-E51FD46BFE3A",
            "Contacts":[
               {
                  "AccountID":"C31ACAD6-0E5D-43E0-B1A2-E51FD46BFE3A",
                  "AvatarImage":"http://www.image.com/",
                  "ContactEmail":"a@mail.com",
                  "ContactID":"28fad61f-9a47-41db-888e-19d66de96638",
                  "ContactTitle":"",
                  "DisplayName":"fvdfvdf",
                  "FirstName":"dfvdfv",
                  "LastName":"dfvdfv",
                  "ManagedByStakeholderID":"97e5f0d5-df0d-48fe-bbbb-cccf251f4175",
                  "Notes":"",
                  "Roles":[
                     
                  ],
                  "StakeholderID":"43d3af87-fd21-404c-9af6-2998e401fcb8",
                  "id":"28fad61f-9a47-41db-888e-19d66de96638"
               }
            ],
            "DisplayName":"adidas "
         },
         {
            "AccountID":"A4FBDC11-AB07-47BD-8364-0B1D81D8E38F",
            "Contacts":[
               {
                  "AccountID":"A4FBDC11-AB07-47BD-8364-0B1D81D8E38F",
                  "AvatarImage":"http://www.image.com/",
                  "ContactEmail":"jeff@linkedin.com",
                  "ContactID":"c205d05d-c4cf-46b2-a9fd-e85af2f317b9",
                  "ContactTitle":"",
                  "DisplayName":"Jeff Weiner",
                  "FirstName":"Jeff",
                  "LastName":"Weiner",
                  "ManagedByStakeholderID":"97e5f0d5-df0d-48fe-bbbb-cccf251f4175",
                  "Notes":"",
                  "Roles":[
                     
                  ],
                  "StakeholderID":"c6fc9b6e-f857-4ca1-8352-dc55b05a9373",
                  "id":"c205d05d-c4cf-46b2-a9fd-e85af2f317b9"
               }
            ],
            "DisplayName":"LinkedIn. Inc "
         }
      ]
   }
"""

do {
    let contacts = try JSONDecoder().decode(Response.self, from: Data(jsonResponse.utf8)).accounts.flatMap(\.contacts)
    print(contacts) // [__lldb_expr_79.Contact(firstName: "Ch", lastName: "Baco"), __lldb_expr_79.Contact(firstName: "dfvdfv", lastName: "dfvdfv"), __lldb_expr_79.Contact(firstName: "Jeff", lastName: "Weiner")]
} catch { print(error) }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多