我正在为帖子API请求json:
{"TokenId": "xxxxxx-xxxxx-xxxx-xxx",
"ObjSearch":
{
"UserId":"0",
"FromDate":"",
"StateId":"0",
"DistrictId":"0",
"ToDate":""
}
}
目前,我已经创建了两个请求模型,例如:
class ChartsReqModel: NSObject, Mappable {
var TokenId:String?
var ObjSearch : [ChartObjSearchReqModel]?
}
和
class ChartObjSearchReqModel: NSObject,Mappable {
var FromDate:String?
var ToDate:String?
var StateId:String?
var DistrictId:String?
var UserId:String?
var ProductId:String?
var SalesChannelId:String?
var ReporteesId:String?
var `Type`:String?
func mapping(map: Map) {
FromDate <- map["FromDate"];
ToDate <- map["ToDate"];
StateId <- map["StateId"];
DistrictId <- map["DistrictId"];
UserId <- map["UserId"];
ProductId <- map["ProductId"];
SalesChannelId <- map["SalesChannelId"];
ReporteesId <- map["ReporteesId"];
Type <- map[![[][1]][1]"Type"];
}
}
我从邮递员中的 API 获得的所需响应看起来很完美,但我认为从我的代码中我无法发送正确的请求模型,并且对于我的数据显示
数据 = ( );
我是 Swift 的新手,我无法找出代码中的问题。 请有人帮我找到解决方案
邮递员截图:
基于请求结构,以下是对对象进行编码/解码ChartsReqModel
简化方法,
class ChartsReqModel: NSObject, Mappable {
var TokenId:String?
var ObjSearch: ChartObjSearchReqModel?
required init?(map: Map) {}
func mapping(map: Map) {
self.TokenId <- map["TokenId"]
self.ObjSearch <- map["ObjSearch"]
}
}
现在,当您从响应中json
时,您可以通过传递该 json 来解码ChartsReqModel
对象,如下所示,
let chartsObject = ChartsReqModel(JSON: jsonFromResponse)
当你想发布ChartsReqModel
的json时,你可以按如下方式编码
let paramsJson = chartsObject.toJSON()