从 Json 数据分组,其中包含 Swift 中的字典数组 4

Grouping from Json data, which contain array of dictionaries in Swift 4

我有本地 json 文件,其中包含字典数组。我想根据 json 下面的键名进行分组。表示一组中的相同名称。请告诉我我怎样才能做到这一点。谢谢你。

Json数据:

[ { "name": "Abc", "number": 123, "marks": 78 }, { "name": "xyz", "number": 456, "marks": 50 }, { "name": "Abc", "number": 789, "marks": 78 } ]

代码:

code and error message

init(分组:按:)

您应该 return 闭包中 name 键的值。

 let arr = [ [ "name": "Abc", "number": 123, "marks": 78 ], [ "name": "xyz", "number": 456, "marks": 50 ], [ "name": "Abc", "number": 789, "marks": 78 ] ]
 let dict = Dictionary(grouping: arr) { [=10=]["name"] as! String }
 print(dict)

//["Abc": [["name": "Abc", "number": 123, "marks": 78], ["name": "Abc", "number": 789, "marks": 78]], "xyz": [["name": "xyz", "number": 456, "marks": 50]]]