我有一个问题,我无法在SWIFT 3.0中将数组转换为JSON。我使用ObjectMapper将数组<CustomObject>转换为JSON
我的目标
class OrderItem: Mappable {
required init?(map: Map) {
}
init() {
}
var oi_id: Int? = 0
var quantity: Double? = 0.0
var discount: Double = 0.0
var sku: Int? = 0
var orderId: Int? = 0
var Product: Product?
var isAdd: Bool = false
var isMissing: Bool!
func mapping(map: Map) {
oi_id <- map["oi_id"]
quantity <- map["quantity"]
discount <- map["discount"]
orderId <- map["orderId"]
Product <- map["Product"]
isAdd <- map["isAdd"]
isMissing <- map["isMissing"]
}}
转换/生成结果JSON:
var jsonArrayOrderItem = arrayOrderItem.toJSON()
结果JSON转换后。 JSON是错误的:(
[["oi_id": 0, "isAdd": false, "quantity": 1.0, "Product": ["isHot": false, "discount": 50.0, "description": "description", "Acronym": "kg", "priceWithDiscount": 0.62, "bigValue": 1.0, "sku": 14, "Name": "Green Apples", "Price": 1.23, "IsFavorite": true, "smallValue": 0.20000000000000001, "Category": ["bigImageUrl": "https://i.imgur.com/7R3sFnP.png", "ImageUrl": "https://i.imgur.com/NJP4CuA.png", "cat_id": 11, "Name": "Fruits & vegetables", "Products": []], "MeasurementUnitId": 0, "ImageUrl": "https://s22.postimg.org/5992ux3j5/Green_apples.jpg", "CategoryId": 11, "Brand": ["brand_id": 8, "name": "Arbella", "imageUrl": "http://i.imgur.com/xx5ZAgL.jpg", "Products": []]], "orderId": 0, "discount": 0.0], ["oi_id": 0, "isAdd": false, "quantity": 1.0, "Product": ["isHot": false, "discount": 50.0, "description": "description", "Acronym": "kg", "priceWithDiscount": 0.62, "bigValue": 1.0, "sku": 17, "Name": "Mango", "Price": 1.23, "IsFavorite": true, "smallValue": 0.20000000000000001, "Category": ["bigImageUrl": "https://i.imgur.com/7R3sFnP.png", "ImageUrl": "https://i.imgur.com/NJP4CuA.png", "cat_id": 11, "Name": "Fruits & vegetables", "Products": []], "MeasurementUnitId": 0, "ImageUrl": "https://s22.postimg.org/4om6zb2zl/Mango.jpg", "CategoryId": 11, "Brand": ["brand_id": 8, "name": "Arbella", "imageUrl": "http://i.imgur.com/xx5ZAgL.jpg", "Products": []]], "orderId": 0, "discount": 0.0]]
结果JSON是怎么回事? – Callam
这是不正确的,请尝试插入JSON到在线转换器@Callam –
什么是ListOrderItem?你有代码吗? – Callam