gpt4 book ai didi

json - Swift:无法解析对模型的 JSON 响应

转载 作者:行者123 更新时间:2023-11-28 11:37:03 28 4
gpt4 key购买 nike

我正在从服务器获取数据响应,如下所示:

{
"success": true,
"data": {
"cash": 0,
"newCash": 0,
"cashExpireAt": "2019-02-26T16:01:35.6451887+08:00",
"isEnabled": false,
"low_balance": true
}
}

//下面是模型类

class WalletBalance: Mappable {

var isSuccess : Bool! = false
var data : WalletData?

required init?(map: Map) {
//Code here
}

func mapping(map: Map) {
isSuccess <- map["success"]
data <- map["data"]
}
}

class WalletData: Mappable {

var cash : Int! = 0
var newCash : Int! = 0
var cashExpireAt : String! = ""
var isEnabled : Bool! = false
var low_balance : Bool! = false

required init?(map: Map) {
//Code here
}

func mapping(map: Map) {
cash <- map["cash"]
newCash <- map["newCash"]
cashExpireAt <- map["cashExpireAt"]
isEnabled <- map["isEnabled"]
low_balance <- map["low_balance"]
}
}

//下面是调用服务端api的代码

Alamofire.request(url!, method: .get, parameters: nil, encoding: URLEncoding.default, headers: walletHeaders)
.validate(statusCode: 200..<600)
.responseObject { (response: DataResponse<WalletBalance>) in
switch response.result {
case .success:
if response.result.value == nil {
//Code here
} else {
completion(response.result.value!)
}
break
case .failure(let error):
print("error: ", error.localizedDescription)
break
}
}

我遇到错误:

error: The operation couldn’t be completed. ObjectMapper failed to serialize response.

我尝试使用 postman 调用 api,但它在 Postman 中运行良好。

最佳答案

您是否尝试过使用 Decodable 而不是 Alamofire documentation 建议的 Mappable

关于json - Swift:无法解析对模型的 JSON 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54881319/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com