objectMapper 和 Alamofire 如何 post 嵌套 json?
How to post nested json by objectMapper and Alamofire?
我正在请求 json post API :
{"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"];
}
}
我在 postman 中从我的 API 得到的期望响应看起来很完美,但我认为从我的代码来看我无法发送正确的请求模型并且我的数据显示
Data = (
);
我是 Swift 的新手,我无法找出代码中的问题..
请有人帮我找到解决方案
POSTMAN 截图:
根据请求结构,下面是encode/decodeChartsReqModel
对象的简化方法,
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)
而当你想要post ChartsReqModel
json 时,你可以将其编码如下
let paramsJson = chartsObject.toJSON()
我正在请求 json post API :
{"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"];
}
}
我在 postman 中从我的 API 得到的期望响应看起来很完美,但我认为从我的代码来看我无法发送正确的请求模型并且我的数据显示
Data = ( );
我是 Swift 的新手,我无法找出代码中的问题.. 请有人帮我找到解决方案
POSTMAN 截图:
根据请求结构,下面是encode/decodeChartsReqModel
对象的简化方法,
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)
而当你想要post ChartsReqModel
json 时,你可以将其编码如下
let paramsJson = chartsObject.toJSON()