将Array <customobject>转换为JSON

时间:2017-08-09 20:18:09

标签: ios swift swift3

我有一个问题我无法在SWIFT 3.0中将数组转换为JSON。我正在使用ObjectMapper

我的对象

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]]

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:-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
    }
]