我有这种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_id
,size_id
和quantity
。为此,我创建了这个结构:
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")])]
我做错了什么?如何将新数量添加到现有元素中?感谢代码示例或任何帮助。
答案 0 :(得分:2)
在Swift 4中,SwiftyJSON
已过时,而受Codable
协议的支持。
不要在结构中使用元组,请分隔值并将Product
(最好为单数形式)实例分组为字典,具体取决于产品ID。
假设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) }
答案 1 :(得分:1)
要在这样的循环中进行更轻松的修改,应将Products
设为一个类,因为它是通过引用传递的。另外,将quantities
中的var
设为Products
,并将解析代码替换为:
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({ $0.id == productId }).first {
product.quantities?.append(quantity)
} else {
self.productWithQuantites.append(Products(id: productId, quantities: [quantity]))
}
}
此代码将新的元组附加到现有产品的quantities
属性,或者如果不存在,则创建新的Products
并将其附加到productWithQuantites
。
答案 2 :(得分:0)
这样接收是正常的,因为在您的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)
}
}
}
答案 3 :(得分:0)
您应该将Alamofire与JSONDecoder结合使用,而不是swiftyjson,更多参考可以观看此视频,该视频在此链接https://m.youtube.com/watch?v=YY3bTxgxWss
中可用