我们有如下数组的json对象:
[{
"CompanyCode": "1",
"MachineCode": null,
"MachineName": null,
"NoOfMeters": null,
"ScheduleNo": "12028",
"ScheduleDate": "17/2/2018 12:00:00 AM",
"ScheduleUser": "sales",
"CustomerCode": "0022100007",
"DeliveryCode": "0",
"SortOrder": "1",
"JobRefNo": "",
"CustomerName": "COLD STORAGE SUPERMARKETS ",
"CustAddress1": "GIANT COMPLEX",
"OutletName": "",
"OutAddress1": "",
"IsJobClosed": ""
}, {
"CompanyCode": "1",
"MachineCode": null,
"MachineName": null,
"NoOfMeters": null,
"ScheduleNo": "12029",
"ScheduleDate": "17/2/2018 12:00:00 AM",
"ScheduleUser": "sales",
"CustomerCode": "0022100008",
"DeliveryCode": "0",
"SortOrder": "1",
"JobRefNo": "",
"CustomerName": "COLD STORAGE WH - 21 TAMPINES ",
"CustAddress1": "GIANT COMPLEX",
"OutletName": "",
"OutAddress1": "",
"IsJobClosed": ""
}, {
"CompanyCode": "1",
"MachineCode": null,
"MachineName": null,
"NoOfMeters": null,
"ScheduleNo": "12027",
"ScheduleDate": "17/2/2018 12:00:00 AM",
"ScheduleUser": "winapp",
"CustomerCode": "0022100003",
"DeliveryCode": "0",
"SortOrder": "1",
"JobRefNo": "",
"CustomerName": "AVENZA PTE LTD ",
"CustAddress1": "83 CLEMENCEAU AVE",
"OutletName": "",
"OutAddress1": "",
"IsJobClosed": ""
}, {
"CompanyCode": "1",
"MachineCode": null,
"MachineName": null,
"NoOfMeters": null,
"ScheduleNo": "12025",
"ScheduleDate": "17/2/2018 12:00:00 AM",
"ScheduleUser": "winapp",
"CustomerCode": "0022100001",
"DeliveryCode": "0",
"SortOrder": "1",
"JobRefNo": "",
"CustomerName": "CASH ON DELIVERY (TO) ",
"CustAddress1": "CASH ON DELIVERY",
"OutletName": "",
"OutAddress1": "",
"IsJobClosed": ""
}, {
"CompanyCode": "1",
"MachineCode": null,
"MachineName": null,
"NoOfMeters": null,
"ScheduleNo": "12026",
"ScheduleDate": "17/2/2018 12:00:00 AM",
"ScheduleUser": "winapp",
"CustomerCode": "0022100002",
"DeliveryCode": "0",
"SortOrder": "1",
"JobRefNo": "",
"CustomerName": "DESPATCH - MT ",
"CustAddress1": "DESPATCH",
"OutletName": "",
"OutAddress1": "",
"IsJobClosed": ""
}]
我们希望根据 ScheduleUser 键(如一节)对这个数组进行分组。
[
"Sales":[
{
}
{
}
]
"winapp":[
{
}
{
}
{
}
]
]
我们已经研究了关于快速4分组Here
但是他们给定了字符串数组,我们不知道要使用json值。
如果有其他方法,请给我们解决方案。我们引用了以下Link
我们无法理解这一点。
从本地获取价值
if let path = Bundle.main.path(forResource: "salesorder", ofType: "json") {
do {
let data = try Data(contentsOf: URL(fileURLWithPath: path), options: .alwaysMapped)
do{
let json = try JSONSerialization.jsonObject(with: data, options: .allowFragments)
let jsonDictionary = json as? [[String:Any]]
print(jsonDictionary!)
}catch let error{
print(error.localizedDescription)
}
} catch let error {
print(error.localizedDescription)
}
} else {
print("Invalid filename/path.")
}
提前致谢..
最佳答案
更好
使用分组方式
if let path = Bundle.main.path(forResource: "salesorder", ofType: "json") {
do {
let data = try Data(contentsOf: URL(fileURLWithPath: path), options: .alwaysMapped)
do{
let json = try JSONSerialization.jsonObject(with: data, options: .allowFragments)
if let jsonDictionary = json as? [[String:Any]]{
var grouped2 : [String:[[String:Any]]] = Dictionary(grouping: jsonDictionary, by: {$0["ScheduleUser"] as! String})
debugPrint(grouped2)
print(jsonDictionary)
}
}catch let error{
print(error.localizedDescription)
}
} catch let error {
print(error.localizedDescription)
}
} else {
print("Invalid filename/path.")
}
手动方式if let path = Bundle.main.path(forResource: "salesorder", ofType: "json") {
do {
let data = try Data(contentsOf: URL(fileURLWithPath: path), options: .alwaysMapped)
do{
let json = try JSONSerialization.jsonObject(with: data, options: .allowFragments)
if let jsonDictionary = json as? [[String:Any]]{
var grouped : [String:[[String:Any]]] = [:]
for dict in jsonDictionary {
let value = dict["ScheduleUser"] as! String
if(grouped[dict["ScheduleUser"] as! String] == nil){
grouped[value] = [dict]
}else{
grouped[value]?.append(dict)
}
}
debugPrint(grouped)
print(jsonDictionary)
}
}catch let error{
print(error.localizedDescription)
}
} catch let error {
print(error.localizedDescription)
}
} else {
print("Invalid filename/path.")
}
日志[“winapp”:[[“MachineName”:,“CustomerCode”:0022100003,
“JobRefNo”:,“OutAddress1”:,“SortOrder”:1,“IsJobClosed”:,
“CustAddress1”:83 CLEMENCEAU AVE,“OutletName”:,“MachineCode”:
,“ScheduleDate”:17/2/2018 12:00:00 AM,“CustomerName”:AVENZA
PTE LTD,“ScheduleNo”:12027,“NoOfMeters”:,“DeliveryCode”:
0,“ScheduleUser”:winapp,“CompanyCode”:1],[“MachineName”:,
“CustomerCode”:0022100001,“JobRefNo”:,“OutAddress1”:,
“SortOrder”:1,“IsJobClosed”:,“CustAddress1”:现金交付,
“OutletName”:,“MachineCode”:,“ScheduleDate”:17/2/2018
上午12:00:00,“客户名称”:货到付款(TO),“时间表编号”:
12025,“NoOfMeters”:,“DeliveryCode”:0,“ScheduleUser”:
winapp,“CompanyCode”:1],[“MachineName”:,“CustomerCode”:
0022100002,“JobRefNo”:,“OutAddress1”:,“SortOrder”:1,
“IsJobClosed”:,“CustAddress1”:DESPATCH,“OutletName”:,
“MachineCode”:,“ScheduleDate”:17/2/2018 12:00:00 AM,
“客户名称”:DESPATCH-MT,“ScheduleNo”:12026,“NoOfMeters”:
,“DeliveryCode”:0,“ScheduleUser”:winapp,“CompanyCode”:1]],
“sales”:[[“” MachineName“:,” CustomerCode“:0022100007,
“JobRefNo”:,“OutAddress1”:,“SortOrder”:1,“IsJobClosed”:,
“CustAddress1”:GIANT COMPLEX,“OutletName”:,“MachineCode”:,
“ScheduleDate”:17/2/2018 12:00:00 AM,“CustomerName”:冷存储
SUPERMARKETS,“ScheduleNo”:12028,“NoOfMeters”:,
“DeliveryCode”:0,“ScheduleUser”:销售,“CompanyCode”:1],
[“MachineName”:,“CustomerCode”:0022100008,“JobRefNo”:,
“OutAddress1”:,“SortOrder”:1,“IsJobClosed”:“CustAddress1”:
GIANT COMPLEX,“OutletName”:,“MachineCode”:,“ScheduleDate”:
17/2/2018 12:00:00 AM,“CustomerName”:冷存储WH-21 TAMPINES,
“ScheduleNo”:12029,“NoOfMeters”:,“DeliveryCode”:0,
“ScheduleUser”:销售,“CompanyCode”:1]]]]