gpt4 book ai didi

json - 使用 SwiftyJSON 解析具有相似元素的 JSON

转载 作者:行者123 更新时间:2023-11-28 10:37:11 28 4
gpt4 key购买 nike

我有这种类型的 JSON(只是一个简短的示例):

"orderProducts": [
{
"id": 135,
"order_id": 39,
"product_id": 49,
"product_code": "",
"size_id": 13,
"quantity": 2,
"product": {
"id": 49,
"code": "",
"factory_id": 2,
"product_category_id": 1,
"sex_id": null,
"season_id": null,
"product_type_id": null,
"type_id": null,
"color_id": null,
"year_id": null,
"image_1": "bceef8b28ae2a1797ca0c6300021100d.jpeg",
"image_2": "",
"image_3": "",
"notes": "",
"status": 10
}
},
{
"id": 136,
"order_id": 39,
"product_id": 49,
"product_code": "",
"size_id": 14,
"quantity": 3,
"product": {
"id": 49,
"code": "",
"factory_id": 2,
"product_category_id": 1,
"sex_id": null,
"season_id": null,
"product_type_id": null,
"type_id": null,
"color_id": null,
"year_id": null,
"image_1": "bceef8b28ae2a1797ca0c6300021100d.jpeg",
"image_2": "",
"image_3": "",
"notes": "",
"status": 10
}
},
{
"id": 137,
"order_id": 39,
"product_id": 48,
"product_code": "",
"size_id": null,
"quantity": 24,
"product": {
"id": 48,
"code": "",
"factory_id": 2,
"product_category_id": null,
"sex_id": null,
"season_id": null,
"product_type_id": null,
"type_id": null,
"color_id": null,
"year_id": null,
"image_1": "2aee8660b4218bf549c2d9345beb2a01.jpeg",
"image_2": "",
"image_3": "",
"notes": "",
"status": 10
}
}]

我需要解析的项目是:product_idsize_idquantity。为此,我创建了这个结构:

struct Products {
let id: String
let quantities: [(sizeId: String, quantity: String)]?

init(id: String, quantities: [(sizeId: String, quantity: String)]) {
self.id = id
self.quantities = quantities
}
}

我想要达到的最终结果是:

[Products(id: "49", quantities: [(sizeId: "13", quantity: "2"), (sizeId: "14", quantity: "3")]), 
Products(id: "48", quantities: [(sizeId: "null", quantity: "24")])]

当使用 SwiftyJSON 解析时,我这样做:

for productId in products.arrayValue {
self.productWithQuantites.append(Products(id: productId["product_id"].stringValue,
quantities: [(sizeId: productId["size_id"].stringValue,
quantity: productId["quantity"].stringValue)]))

}

但是我明白了:

    [Products(id: "49", quantities: [(sizeId: "13", quantity: "2")]),
Products(id: "49", quantities: [(sizeId: "14", quantity: "3")],
Products(id: "48", quantities: [(sizeId: "null", quantity: "24")])]

我做错了什么?如何将新数量附加到现有元素?非常感谢您提供代码示例或任何帮助。

最佳答案

在 Swift 4 中,SwiftyJSON 已过时,取而代之的是 Codable 协议(protocol)。

不要在结构中使用元组,将值分开并根据产品 ID 将 Product(最好是单数形式)实例分组到字典中。

假设 data 包含 JSON 原始数据创建这些微型结构

struct Root : Decodable {
let orderProducts : [Product]
}

struct Product: Decodable {
let id : Int
let productId : Int
let sizeId : Int?
}

将 JSON 解码为结构并使用 Dictionary(grouping:by:) 对数组进行分组

do {
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
let result = try decoder.decode(Root.self, from: data)
let groupedDictionary = Dictionary(grouping: result.orderProducts, by: {$0.productId})
print(groupedDictionary)
// [49: [Product(id: 135, productId: 49, sizeId: Optional(13)),
// Product(id: 136, productId: 49, sizeId: Optional(14))],
// 48: [Product(id: 137, productId: 48, sizeId: nil)]]

} catch { print(error) }

关于json - 使用 SwiftyJSON 解析具有相似元素的 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52840755/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com