“ typeMismatch(Swift.Dictionary <swift.string,any =”“> debugDescription:”预期对Dictionary <string,any =“”>进行解码,但是找到了一个数组。”

时间:2018-12-14 10:05:20

标签: ios swift dictionary realm decodable

现在我遇到这个错误

  

“ [UserOrderHistory]”类型的值没有成员“订单列表”

我的JSON:

{
"OrderList": [
    {
        "orderId": 16976,
        "userId": 4905,
        "pickupdate": "2018-09-23",
    },
    {
        "orderId": 52,
        "userId": 4905,
        "pickupdate": "2018-08-07",
    },
],
"TotalOrder": 2
}

我的可分解模型类:

    class UserOrderHistory: Object, Decodable {

      var orderlist: [OrderList]?
      var TotalO = RealmOptional<Int>()

     enum CodingKeys: String, CodingKey {
        case orderlist = "OrderList"
        case TotalO = "TotalOrder"
     }
convenience required init(from decoder: Decoder) throws {
    self.init()
    let container = try decoder.container(keyedBy: CodingKeys.self)
    self.orderlist = try container.decodeIfPresent(OrderList.self, forKey: .orderlist)
    self.TotalO.value = try container.decodeIfPresent(Int.self, forKey: .TotalO)
}

required init() {
    super.init()
}

required init(value: Any, schema: RLMSchema) {
    super.init(value: value, schema: schema)
}

required init(realm: RLMRealm, schema: RLMObjectSchema) {
    super.init(realm: realm, schema: schema)
}
}


class OrderList: Object, Decodable{
var orderId = RealmOptional<Int>()
var userId = RealmOptional<Int>()
@objc dynamic var pickupdate: String? = nil

enum CatCodingKeys: String, CodingKey {
    case orderId
    case userId
    case pickupdate
}

convenience required init(from decoder: Decoder) throws {
    self.init()
    let container = try decoder.container(keyedBy: CatCodingKeys.self)
    self.orderId.value = try container.decodeIfPresent(Int.self, forKey: .orderId)
    self.userId.value = try container.decodeIfPresent(Int.self, forKey: .userId)

    self.pickupdate = try container.decodeIfPresent(String.self, forKey: .pickupdate)

}

required init() {
    super.init()
}

required init(value: Any, schema: RLMSchema) {
    super.init(value: value, schema: schema)
}

required init(realm: RLMRealm, schema: RLMObjectSchema) {
    super.init(realm: realm, schema: schema)
}
  }

将服务器端数据解码为与领域兼容:

let decoder = JSONDecoder()
            do {
            let orders = try decoder.decode(Array<UserOrderHistory>.self, from: data)

                try? realm!.write {
                    realm?.add(orders.orderlist!)
                }

            }catch {
                 print(error)
            }

问题出在哪里。

  
      
  1. 我该如何解决。
  2.   
  3. 是否有其他简便方法可以将这种json类型解析为可解码的。
  4.   

最上面要提到错误。

3 个答案:

答案 0 :(得分:0)

OrderList是一个array。您需要在orderList类中更改变量UserOrderHistory

var orderlist: [OrderList]?

仅像以前一样保留此行,请删除Array

let orders = try decoder.decode(Array<UserOrderHistory>.self, from: data)

至:

let orders = try decoder.decode(UserOrderHistory.self, from: data)

答案 1 :(得分:0)

以下json对象仅对应于UserOrderHistory的一个实例:

{
"OrderList": [
    {
        "orderId": 16976,
        "userId": 4905,
        "pickupdate": "2018-09-23",
    },
    {
        "orderId": 52,
        "userId": 4905,
        "pickupdate": "2018-08-07",
    },
],
"TotalOrder": 2
}

因此使用以下内容进行解码就足够了:

let orders = try decoder.decode(UserOrderHistory.self, from: data)

您正在尝试按照我看到的方式从对象解码数组,否则将失败。

答案 2 :(得分:-1)

能否请您尝试使用Array<UserOrderHistory>.self代替UserOrderHistory.self

在您的UserOrderHistory类中,

var orderlist: [OrderList]?

所以代码应该看起来像

let decoder = JSONDecoder()
        do {
        let orders = try decoder.decode(Array<UserOrderHistory>.self, from: data)

            try? realm!.write {
                realm?.add(orders.orderlist!)
            }

        }catch {
             print(error)
        }