如果对象包含基于键的重复名称,则对 json 个对象进行分组?
Grouping json objects if object contains duplicate name based on key?
我们有如下 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":[
{
}
{
}
{
}
]
]
我们在 swift 4 Here
中研究了分组
但是他们给出了字符串数组,我们不知道如何处理 json 值。
如果有任何其他方式,请给我们解决方案。我们参考了以下
我们无法理解这一点。
从本地获取值
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: {[=10=]["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": CASH ON DELIVERY,
"OutletName": , "MachineCode": , "ScheduleDate": 17/2/2018
12:00:00 AM, "CustomerName": CASH ON DELIVERY (TO) , "ScheduleNo":
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,
"CustomerName": 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": COLD STORAGE
SUPERMARKETS , "ScheduleNo": 12028, "NoOfMeters": ,
"DeliveryCode": 0, "ScheduleUser": sales, "CompanyCode": 1],
["MachineName": , "CustomerCode": 0022100008, "JobRefNo": ,
"OutAddress1": , "SortOrder": 1, "IsJobClosed": , "CustAddress1":
GIANT COMPLEX, "OutletName": , "MachineCode": , "ScheduleDate":
17/2/2018 12:00:00 AM, "CustomerName": COLD STORAGE WH - 21 TAMPINES ,
"ScheduleNo": 12029, "NoOfMeters": , "DeliveryCode": 0,
"ScheduleUser": sales, "CompanyCode": 1]]]
获取数组中所有可能的值 -
let arr = your data...
let posValues = arr.map { [=10=]["ScheduleUser"] as? String }
现在为过滤数据创建字典 -
var newDict:[String:Any] = [:]
for aValue in posValues {
let filteredDicts = arr.filter({ ([=11=].["ScheduleUser"] == aValue)})
newDict[aValue] = filteredDicts
}
我们有如下 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":[
{
}
{
}
{
}
]
]
我们在 swift 4 Here
中研究了分组但是他们给出了字符串数组,我们不知道如何处理 json 值。
如果有任何其他方式,请给我们解决方案。我们参考了以下
我们无法理解这一点。
从本地获取值
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: {[=10=]["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": CASH ON DELIVERY, "OutletName": , "MachineCode": , "ScheduleDate": 17/2/2018 12:00:00 AM, "CustomerName": CASH ON DELIVERY (TO) , "ScheduleNo": 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, "CustomerName": 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": COLD STORAGE SUPERMARKETS , "ScheduleNo": 12028, "NoOfMeters": , "DeliveryCode": 0, "ScheduleUser": sales, "CompanyCode": 1], ["MachineName": , "CustomerCode": 0022100008, "JobRefNo": , "OutAddress1": , "SortOrder": 1, "IsJobClosed": , "CustAddress1": GIANT COMPLEX, "OutletName": , "MachineCode": , "ScheduleDate": 17/2/2018 12:00:00 AM, "CustomerName": COLD STORAGE WH - 21 TAMPINES , "ScheduleNo": 12029, "NoOfMeters": , "DeliveryCode": 0, "ScheduleUser": sales, "CompanyCode": 1]]]
获取数组中所有可能的值 -
let arr = your data...
let posValues = arr.map { [=10=]["ScheduleUser"] as? String }
现在为过滤数据创建字典 -
var newDict:[String:Any] = [:]
for aValue in posValues {
let filteredDicts = arr.filter({ ([=11=].["ScheduleUser"] == aValue)})
newDict[aValue] = filteredDicts
}