ios - 类型不匹配(Swift.Dictionary<Swift.String, Any> debugDescription : "Expected to decode Dictionary<String, Any> but found an array instead."

标签 ios swift dictionary realm decodable

现在我正面临这个错误

Value of type '[UserOrderHistory]' has no member 'orderlist'

我的 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)
}
  }

将服务器端数据解码为 Realm 兼容:

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. How I can solve this.
  2. Is there any other and easy way to parse this type of json to using decodable.

在顶部提到错误。

最佳答案

OrderList 是一个数组。您需要更改 UserOrderHistory 类中的变量 orderList:

var orderlist: [OrderList]?

仅保留此行,删除 Array:

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

到:

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

关于ios - 类型不匹配(Swift.Dictionary<Swift.String, Any> debugDescription : "Expected to decode Dictionary<String, Any> but found an array instead.",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53777511/

相关文章:

ios - Swift 3 类型 'Any' 没有下标成员

Python:替换字典值中的单个字符

ios - 如果UIPasteboard.general.string返回nil,为什么UIPasteboard.general.string?.append(“another”)崩溃?

启用声音的 JavaPNS 推送通知

swift - 我可以使用 ARKit 一次跟踪 4 张以上的图像吗?

ios - Swift:transitionFromView 翻转整个屏幕而不是 View

sql - 删除SQL Server 2008数据库中的字符串重复

angularjs - 是否可以使用 Redis 作为字典和 AngularJs 来实现拼写检查功能?

ios - 将 UI 滑动手势识别添加到自定义 View ios

ios - 如何在uiTableViewCell 内的uiTextField 下画一条线?