我必须向服务器发送请求。保存用户记录。用户记录包含Subject的模型,并且Subject也包含多个模型和数组。让我给你一点例子。
@ignore
public class SubjectModel: Codable {
public var Name : String? = ""
public var Price : Int? = 0
public var ISBN : String? = ""
public var listTeachers : [TeacherModel]? = []
}
public class TeacherModel: Codable {
public var Name : String? = ""
public var PhoneNo : String? = ""
public var listClasses: [ClassModel]? = []
}
public class ClassModel: Codable {
public var Name : String? = ""
public var Section : String? = ""
public var RoomNo : String? = ""
}
因此,我必须发送SubjectModel,如您所见,SubjectModel包含多个InnerModel数组。所有型号都是可编码的。
因此,我的网络服务所需的JSON必须类似于
{ApiKey:“ asas-dsadas-xyz-xyz”,“ StudentSubjects”:{现在到了 数组,“ InnerList”:[这里是内部数组]}}
问题:
请帮助我,我正在ash头,但无法在服务器端得到答复,实际上我得到的是空值。
UPDATE1:这就是我的做法
let jsonEncoder = JSONEncoder()
let jsonData = try jsonEncoder.encode(mySubjectModel)
let json = String(data:jsonData,encoding:String.Encoding.utf8)
let parameter: [String:AnyObject] = [
"StudentSubjects" : json as AnyObject,
"ApiKey":"xyz-zyz-zyz"
]
然后像这样发送
Alamofire.request(Common.URL+ServiceMethodName, method: .post, parameters:parameter, encoding: JSONEncoding.default, headers: nil) .responseJSON { response in
答案 0 :(得分:0)
您需要使用此方法(不确定是否需要PUT或POST,但应该是POST)
Alamofire.request(.POST, "http://myserver.com", parameters: parameters, encoding: .JSON)
.responseJSON { request, response, JSON, error in
print(response)
print(JSON)
print(error)
}
parameters对象是
let parameters: [String: AnyObject] = [
"ApiKey" : "asas-dsadas-xyz-xyz",
"StudentSubjects" : yourStudnetSubjectsList
]
Alamofire将为您完成向JSON的转换。