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

Parse JSON with similar elements using SwiftyJSON

我有这种 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")])]

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

这样收到是正常的,因为在你的JSON中有两个条目,"product_id": 49

所以你可以解决它,在解析你的 json 之后,一旦你多次找到相同的项目,就用两个 for 循环删除:

for i in 0..<productWithQuantites.count {
        for j in 0..<productWithQuantites.count {
            if productWithQuantites[i].productID == productWithQuantites[j].productID {
                productWithQuantites.remove(at: j)
            }
        }
    }

为了在这样的循环中更容易修改,您应该将 Products 设为 class,因为它是通过引用传递的。另外,在你的 Products 中将 quantities 变为 var 并将解析代码替换为:

for product in products.arrayValue {
    let productId = product["product_id"].stringValue

    let quantity = (sizeId: product["size_id"].stringValue,
                    quantity: product["quantity"].stringValue)

    if let product = self.productWithQuantites.filter({ [=10=].id == productId }).first {
        product.quantities?.append(quantity)
    } else {
        self.productWithQuantites.append(Products(id: productId, quantities: [quantity]))
    }
}

此代码将一个新元组附加到现有产品的 quantities 属性,或者如果它不存在,它会创建一个新的 Products 并将其附加到 productWithQuantites.

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

不要在结构中使用元组,将值分开并根据产品 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: {[=11=].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) }

您应该将 Alamofire 与 JSONDecoder 一起使用,而不是 swiftyjson 和更多参考您可以观看此 link https://m.youtube.com/watch?v=YY3bTxgxWss

上提供的视频