Swift - 查找重复的 Json 数据并合并
Swift - Finding duplicate Json data and merging
我正在使用 swiftyjson 来处理我的 json 数据。
目前,我正在接收具有要合并在一起的值的数据。 (如果你看看下面的例子,它会更有意义。我尝试了很多不同的方法,例如使用 for 循环遍历 JSON 数据,但这会复制所有内容。我也尝试过改变我的 json 到二维数组然后过滤它,但这似乎使事情复杂化。必须有更简单的方法...
var jsondata = {
{
fruit: "APPLE"
amount: 10
},{
fruit: "Mango"
amount: 5
},{
fruit: "APPLE"
amount: 5
},{
fruit: "Mango"
amount: 5
},{
fruit: "orange"
amount: 500
}
}
var NEWjsondata =
{
{
fruit: "APPLE"
amount: 15
},
{
fruit: "Mango"
amount: 10
},
{
fruit: "orange"
amount: 500
}
}
我的做法
var arr = ["Apple","Mango","Orange"]
for (key,json) in jsondata {
arr.append(json["fruit"])
if arr.contains(json["fruit"]){
json["amount"] = json["amount"] + json["amount"]
}}
我正在浏览所有词典并从每个水果名称和数量中提取,然后在结果词典中创建一个新条目或添加到已经计算过的先前数量。
func mergedDict(dictionaries: [[String: Any]]) -> [String: Int] {
guard dictionaries.count != 0 else { return [:] }
var result = [String: Int]()
dictionaries.forEach { dict in
var name: String?
var amount: Int?
dict.forEach{ arg in
switch arg.key {
case "fruit":
name = arg.value as! String
case "amount":
amount = arg.value as! Int
default:
fatalError("does not exist.")
}
}
guard let fruit = name else { fatalError("name is nil.") }
guard let newAmount = amount else { fatalError("amount is nil.") }
guard let priorAmount = result[fruit] else {
result[fruit] = newAmount
return
}
result[fruit] = priorAmount + newAmount
}
return result
}
使 FruitStruct 符合 Codable 协议允许 encoder.encode 获取我们的 FruitStruct 数组。该结构模仿我们想要的 JSON 。可以添加 enum CodingKeys
以将映射从结构更改为实际的 JSON.
struct FruitStruct: Codable {
let fruit: String
let amount: Int
}
func json(fromDict: [String: Int]) throws -> String {
let fruitStructs = fromDict.map { arg in
return FruitStruct(fruit: arg.key, amount: arg.value)
}
let encoder = JSONEncoder()
encoder.outputFormatting = .prettyPrinted
let data = try encoder.encode(fruitStructs)
guard let string = String(data: data, encoding: .utf8) else {
fatalError("failed to decode json-Data to String with utf8.")
}
return string
}
我正在使用 swiftyjson 来处理我的 json 数据。
目前,我正在接收具有要合并在一起的值的数据。 (如果你看看下面的例子,它会更有意义。我尝试了很多不同的方法,例如使用 for 循环遍历 JSON 数据,但这会复制所有内容。我也尝试过改变我的 json 到二维数组然后过滤它,但这似乎使事情复杂化。必须有更简单的方法...
var jsondata = {
{
fruit: "APPLE"
amount: 10
},{
fruit: "Mango"
amount: 5
},{
fruit: "APPLE"
amount: 5
},{
fruit: "Mango"
amount: 5
},{
fruit: "orange"
amount: 500
}
}
var NEWjsondata =
{
{
fruit: "APPLE"
amount: 15
},
{
fruit: "Mango"
amount: 10
},
{
fruit: "orange"
amount: 500
}
}
我的做法
var arr = ["Apple","Mango","Orange"]
for (key,json) in jsondata {
arr.append(json["fruit"])
if arr.contains(json["fruit"]){
json["amount"] = json["amount"] + json["amount"]
}}
我正在浏览所有词典并从每个水果名称和数量中提取,然后在结果词典中创建一个新条目或添加到已经计算过的先前数量。
func mergedDict(dictionaries: [[String: Any]]) -> [String: Int] {
guard dictionaries.count != 0 else { return [:] }
var result = [String: Int]()
dictionaries.forEach { dict in
var name: String?
var amount: Int?
dict.forEach{ arg in
switch arg.key {
case "fruit":
name = arg.value as! String
case "amount":
amount = arg.value as! Int
default:
fatalError("does not exist.")
}
}
guard let fruit = name else { fatalError("name is nil.") }
guard let newAmount = amount else { fatalError("amount is nil.") }
guard let priorAmount = result[fruit] else {
result[fruit] = newAmount
return
}
result[fruit] = priorAmount + newAmount
}
return result
}
使 FruitStruct 符合 Codable 协议允许 encoder.encode 获取我们的 FruitStruct 数组。该结构模仿我们想要的 JSON 。可以添加 enum CodingKeys
以将映射从结构更改为实际的 JSON.
struct FruitStruct: Codable {
let fruit: String
let amount: Int
}
func json(fromDict: [String: Int]) throws -> String {
let fruitStructs = fromDict.map { arg in
return FruitStruct(fruit: arg.key, amount: arg.value)
}
let encoder = JSONEncoder()
encoder.outputFormatting = .prettyPrinted
let data = try encoder.encode(fruitStructs)
guard let string = String(data: data, encoding: .utf8) else {
fatalError("failed to decode json-Data to String with utf8.")
}
return string
}