2017-08-09 35 views
-3

我有一个问题,我无法在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]] 
+0

结果JSON是怎么回事? – Callam

+0

这是不正确的,请尝试插入JSON到在线转换器@Callam –

+0

什么是ListOrderItem?你有代码吗? – Callam

回答

-1

我知道我不是想在回答要求澄清,但我没有足够的信誉发表意见。你能告诉里面有什么arrayOrderItem

我测试。与ObjectMapper你的代码,输出JSON我是正确的这是我做了什么(我用你有完全相同的OrderItem类,除了我创建了自己Product类,符合Mappable):

let orderItem1 = OrderItem() 
let orderItem2 = OrderItem() 

let product1 = Product(id: 0, name: "name") 
let product2 = Product(id: 1, name: "name1") 

orderItem1.Product = product1 
orderItem2.Product = product2 

let arrayOrderItem = [orderItem1, orderItem2] 

let jsonArrayOrderItem = arrayOrderItem.toJSONString(prettyPrint: true)! 

而且这里是我的JSON

[ 
    { 
     "oi_id" : 0, 
     "isAdd" : false, 
     "quantity" : 0, 
     "Product" : { 
      "id" : 0, 
      "name" : "name" 
     }, 
     "orderId" : 0, 
     "discount" : 0 
    }, 
    { 
     "oi_id" : 0, 
     "isAdd" : false, 
     "quantity" : 0, 
     "Product" : { 
      "id" : 1, 
      "name" : "name1" 
     }, 
     "orderId" : 0, 
     "discount" : 0 
    } 
] 
0

尝试使用Swift 4.0 - 它内置了JSONDecoder。