【问题标题】:Convert Array<CustomObject> to JSON将 Array<CustomObject> 转换为 JSON
【发布时间】:2017-08-09 20:18:09
【问题描述】:

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

【问题讨论】:

  • 结果 JSON 有什么问题?
  • 这不正确,尝试将 JSON 插入在线转换器@Callam
  • 什么是ListOrderItem?你有它的代码吗?
  • 你是如何生成结果 JSON 的?
  • 我更正了 ListOrderItem = array 。 @Callam

标签: ios swift swift3


【解决方案1】:

尝试 Swift 4.0 - 它内置了 JSONDecoder。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-09
    • 2018-09-10
    • 1970-01-01
    • 2019-07-12
    • 2017-12-29
    相关资源
    最近更新 更多