提问人:Bappaditya 提问时间:4/25/2022 最后编辑:Bappaditya 更新时间:4/25/2022 访问量:134
如何在 Swift 字典内的字典数组中插入键值对
How to insert a key value pair in an array of dictionary which is inside a dictionary in Swift
问:
我们有一个 JSON 响应,应该在保存到 Coredata 之前进行解析,但是我们想在保存之前解析它并进行一些修改。
[{
"mailId": "316054287",
"attachedDocumentCount": 3,
"attachmentFileSize": 2932927,
"confidential": false,
"correspondenceType": "Transmittal",
"mailNo": "AFDE-TRANSMIT-058698",
"toStatusId": "3",
"corrTypeId": "23",
"reasonForIssue": "Client \"Proceed Subject ot Conditions, Revise and Resubmit\"",
"referenceNumber": "Rebar-TRANSMIT-003389",
"sentDate": "2022-04-21T15:02:42.637Z",
"status": "N/A",
"subject": "C2 - BCH Transmittal - Rebar Shop Drawings SW-EL-CH-07A & 07B - Rev 1 - Review Complete",
"recipients": {
"to": [
{
"organizationId": "268497899",
"organizationName": "AFDE",
"status": "N/A",
"statusId": "3",
"distributionType": "TO",
"firstName": "Tony",
"name": "Mr Tony Fournier",
"userId": "268832810",
"lastName": "Fournier",
"isRead": true
},
{
"organizationId": "268446975",
"organizationName": "Harris Rebar",
"status": "N/A",
"statusId": "3",
"distributionType": "TO",
"firstName": "Melanie",
"name": "Mrs Melanie Coffey",
"userId": "268841518",
"lastName": "Coffey",
"isRead": false
},
{
"organizationId": "268497899",
"organizationName": "AFDE",
"status": "N/A",
"statusId": "3",
"distributionType": "TO",
"firstName": "Caroline",
"name": "Ms Caroline Rancourt",
"userId": "268842732",
"lastName": "Rancourt",
"isRead": true
},
{
"organizationId": "268497899",
"organizationName": "AFDE",
"status": "N/A",
"statusId": "3",
"distributionType": "TO",
"firstName": "Mathieu",
"name": "Mr Mathieu Forcier",
"userId": "268844353",
"lastName": "Forcier",
"isRead": true
},
{
"organizationId": "268497899",
"organizationName": "AFDE",
"status": "N/A",
"statusId": "3",
"distributionType": "TO",
"firstName": "Laurent",
"name": "Mr Laurent Gervais",
"userId": "268848608",
"lastName": "Gervais",
"isRead": true
},
{
"organizationId": "268446975",
"organizationName": "Harris Rebar",
"status": "N/A",
"statusId": "3",
"distributionType": "TO",
"firstName": "Ken",
"name": "Mr Ken Trinidad",
"userId": "268851135",
"lastName": "Trinidad",
"isRead": false
},
{
"organizationId": "268446975",
"organizationName": "Harris Rebar",
"status": "N/A",
"statusId": "3",
"distributionType": "TO",
"firstName": "Brian",
"name": " Brian Webster",
"userId": "268857121",
"lastName": "Webster",
"isRead": false
},
{
"organizationId": "268446975",
"organizationName": "Harris Rebar",
"status": "N/A",
"statusId": "3",
"distributionType": "TO",
"firstName": "Diana",
"name": " Diana Harrison",
"userId": "268863136",
"lastName": "Harrison",
"isRead": false
},
{
"organizationId": "268446975",
"organizationName": "Harris Rebar",
"status": "N/A",
"statusId": "3",
"distributionType": "TO",
"firstName": "Dan",
"name": " Dan Lavallee",
"userId": "268874809",
"lastName": "Lavallee",
"isRead": false
},
{
"organizationId": "268497899",
"organizationName": "AFDE",
"status": "N/A",
"statusId": "3",
"distributionType": "TO",
"firstName": "Cole",
"name": "Mr Cole Daniels",
"userId": "268941632",
"lastName": "Daniels",
"isRead": true
},
{
"organizationId": "268446975",
"organizationName": "Harris Rebar",
"status": "N/A",
"statusId": "3",
"distributionType": "TO",
"firstName": "James",
"name": " James Cross",
"userId": "268968788",
"lastName": "Cross",
"isRead": false
},
{
"organizationId": "268446975",
"organizationName": "Harris Rebar",
"status": "N/A",
"statusId": "3",
"distributionType": "TO",
"firstName": "William",
"name": "Mr William Mint",
"userId": "268968789",
"lastName": "Mint",
"isRead": false
},
{
"organizationId": "268446975",
"organizationName": "Harris Rebar",
"status": "N/A",
"statusId": "3",
"distributionType": "TO",
"firstName": "Greg",
"name": "Mr Greg Smith",
"userId": "268968792",
"lastName": "Smith",
"isRead": false
},
{
"organizationId": "268446975",
"organizationName": "Harris Rebar",
"status": "N/A",
"statusId": "3",
"distributionType": "TO",
"firstName": "Andrew",
"name": "Mr Andrew Rayfield",
"userId": "268978580",
"lastName": "Rayfield",
"isRead": true
},
{
"organizationId": "268446975",
"organizationName": "Harris Rebar",
"status": "N/A",
"statusId": "3",
"distributionType": "TO",
"firstName": "Daniel",
"name": "Mr Daniel Sanderson",
"userId": "269015696",
"lastName": "Sanderson",
"isRead": false
},
{
"organizationId": "268446975",
"organizationName": "Harris Rebar",
"status": "N/A",
"statusId": "3",
"distributionType": "TO",
"firstName": "Vic",
"name": " Vic Levinsky",
"userId": "269017288",
"lastName": "Levinsky",
"isRead": false
},
{
"organizationId": "268446975",
"organizationName": "Harris Rebar",
"status": "N/A",
"statusId": "3",
"distributionType": "TO",
"firstName": "Greg",
"name": " Greg Tait",
"userId": "269029103",
"lastName": "Tait",
"isRead": false
},
{
"organizationId": "268446975",
"organizationName": "Harris Rebar",
"status": "N/A",
"statusId": "3",
"distributionType": "TO",
"firstName": "Brook",
"name": " Brook Alling",
"userId": "269033767",
"lastName": "Alling",
"isRead": false
},
{
"organizationId": "268497899",
"organizationName": "AFDE",
"status": "N/A",
"statusId": "3",
"distributionType": "TO",
"firstName": "Julia",
"name": "Ms Julia Brin",
"userId": "269060417",
"lastName": "Brin",
"isRead": true
},
{
"organizationId": "268446975",
"organizationName": "Harris Rebar",
"status": "N/A",
"statusId": "3",
"distributionType": "TO",
"firstName": "Kevin",
"name": "Mr Kevin Demattos",
"userId": "269097068",
"lastName": "Demattos",
"isRead": false
},
{
"organizationId": "268446975",
"organizationName": "Harris Rebar",
"status": "N/A",
"statusId": "3",
"distributionType": "TO",
"firstName": "Shannon",
"name": "Ms Shannon Rochon",
"userId": "269125638",
"lastName": "Rochon",
"isRead": false
}
],
"cc": [],
"bcc": []
},
"senderDetails": {
"organizationId": "268497899",
"organizationName": "AFDE",
"firstName": "Neil",
"name": "Mr Neil Cantwell",
"userId": "269161169",
"lastName": "Cantwell"
},
"mailBoxType": "inbox",
"hasAttachments": true,
"myMailsOnly": true,
"isReadByMe": false
}]
在这里,我们想从每个邮件对象的顶部键值对中插入,并将其添加到每个收件人对象中,如下所示,mailId
O(1)
O(n)
{
"mailId": "316054287",
"organizationId": "268446975",
"organizationName": "Harris Rebar",
"status": "N/A",
"statusId": "3",
"distributionType": "TO",
"firstName": "Shannon",
"name": "Ms Shannon Rochon",
"userId": "269125638",
"lastName": "Rochon",
"isRead": false
}
我们尝试的代码是同谋的。解析和保存数据大约需要 50 秒。我们希望减少这种情况。O(n2)
// json is saved in `mailResponse` variable
public typealias JSON = [String: Any]
guard let mails = mailResponse["mails"] as? [JSON] else {
return nil
}
if #available(iOS 14.0, *) {
_ = mails.map { mail in
if let recipients = mail["recipients"] as? JSON {
var allRecipients: [JSON] = [JSON]()
if let toRecipients = recipients["to"] as? [JSON] {
allRecipients.append(contentsOf: toRecipients)
}
if let ccRecipients = recipients["cc"] as? [JSON] {
allRecipients.append(contentsOf: ccRecipients)
}
if let bccRecipients = recipients["bcc"] as? [JSON] {
allRecipients.append(contentsOf: bccRecipients)
}
allRecipients = allRecipients.map({ recipient in
var mRecipient = recipient
mRecipient["mailId"] = mail["mailId"]
return mRecipient
})
}
}
}
对此的任何帮助都非常感谢。
答: 暂无答案
下一个:如何解析 url 以获取键和值
评论
mails.map()
map
map