多参数字典(集合列表) Like: [[String: Any]] to Alamofire Parameters
Multi parameters Dictionary (collection list) Like: [[String: Any]] to Alamofire Parameters
您好,我正在尝试向 alamofire 提供字典中名为“Dict”的参数...字典可以包含 3 或 X 个项目。我正在尝试使用 FOR 循环将字典添加到另一组项目,但是......它只显示最后一个......它似乎覆盖了前一个。我尝试了我所知道的一切……甚至尝试使用 SwiftyJSON 框架……但是 alamofire 只采用纯字典类型。
var Dict = [[String: Any]]()
Dict.removeAll()
for (index, value) in _SurveySubmitModel.enumerated() {
print("Item \(index + 1): \(value)")
let parameters: [String: Any] = [
"ID": value.ID,
"SurveyID": value.SurveyID,
"QuestionID": value.QuestionID,
"FilledBy": value.FilledBy,
"Response": value.Response
]
Dict.append(parameters)
}
print("Dict = \(Dict)")
嗯,我需要这样的东西
[{
"ID": 0,
"SurveyID": 25,
"QuestionID": 28,
"FilledBy": 7335,
"Response": "1In the two weeks before you felt sick, did you:"
},
{
"ID": 0,
"SurveyID": 25,
"QuestionID": 28,
"FilledBy": 7335,
"Response": "1In the two weeks before you felt sick, did you:"
}
]
尝试下面的代码,我已经将[String: Any]
修改为NSDictionary
。还更改了 for
循环。
var _SurveySubmitModel = [SurveySubmitModel]()
_SurveySubmitModel.append(SurveySubmitModel(ID: 0, SurveyID: 25, QuestionID: 28, FilledBy: 7335, Response: "1In the two weeks before you felt sick, did you:"))
_SurveySubmitModel.append(SurveySubmitModel(ID: 0, SurveyID: 25, QuestionID: 28, FilledBy: 7335, Response: "1In the two weeks before you felt sick, did you:"))
for survey in _SurveySubmitModel {
let parameters: NSDictionary = [
"ID": survey.ID,
"SurveyID": survey.SurveyID,
"QuestionID": survey.QuestionID,
"FilledBy": survey.FilledBy,
"Response": survey.Response
]
Dict.append(parameters)
}
print("Dict == ", Dict)
输出是
Dict == [{
FilledBy = 7335;
ID = 0;
QuestionID = 28;
Response = "1In the two weeks before you felt sick, did you:";
SurveyID = 25;
}, {
FilledBy = 7335;
ID = 0;
QuestionID = 28;
Response = "1In the two weeks before you felt sick, did you:";
SurveyID = 25;
}]
尝试使用以下函数调用网络服务
func postValues(requestParams: [[String: AnyObject]], urlString: String) {
let url = URL(string: urlString)
var request = URLRequest(url: url!)
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.httpMethod = "POST"
request.httpBody = try! JSONSerialization.data(withJSONObject: requestParams, options: [])
AF.request(request).responseJSON { (response) in
switch response.result {
case .success:
// print(response.result.value)
break
case .failure:
print(response.error)
break
}
}
}
您好,我正在尝试向 alamofire 提供字典中名为“Dict”的参数...字典可以包含 3 或 X 个项目。我正在尝试使用 FOR 循环将字典添加到另一组项目,但是......它只显示最后一个......它似乎覆盖了前一个。我尝试了我所知道的一切……甚至尝试使用 SwiftyJSON 框架……但是 alamofire 只采用纯字典类型。
var Dict = [[String: Any]]()
Dict.removeAll()
for (index, value) in _SurveySubmitModel.enumerated() {
print("Item \(index + 1): \(value)")
let parameters: [String: Any] = [
"ID": value.ID,
"SurveyID": value.SurveyID,
"QuestionID": value.QuestionID,
"FilledBy": value.FilledBy,
"Response": value.Response
]
Dict.append(parameters)
}
print("Dict = \(Dict)")
嗯,我需要这样的东西
[{
"ID": 0,
"SurveyID": 25,
"QuestionID": 28,
"FilledBy": 7335,
"Response": "1In the two weeks before you felt sick, did you:"
},
{
"ID": 0,
"SurveyID": 25,
"QuestionID": 28,
"FilledBy": 7335,
"Response": "1In the two weeks before you felt sick, did you:"
}
]
尝试下面的代码,我已经将[String: Any]
修改为NSDictionary
。还更改了 for
循环。
var _SurveySubmitModel = [SurveySubmitModel]()
_SurveySubmitModel.append(SurveySubmitModel(ID: 0, SurveyID: 25, QuestionID: 28, FilledBy: 7335, Response: "1In the two weeks before you felt sick, did you:"))
_SurveySubmitModel.append(SurveySubmitModel(ID: 0, SurveyID: 25, QuestionID: 28, FilledBy: 7335, Response: "1In the two weeks before you felt sick, did you:"))
for survey in _SurveySubmitModel {
let parameters: NSDictionary = [
"ID": survey.ID,
"SurveyID": survey.SurveyID,
"QuestionID": survey.QuestionID,
"FilledBy": survey.FilledBy,
"Response": survey.Response
]
Dict.append(parameters)
}
print("Dict == ", Dict)
输出是
Dict == [{
FilledBy = 7335;
ID = 0;
QuestionID = 28;
Response = "1In the two weeks before you felt sick, did you:";
SurveyID = 25;
}, {
FilledBy = 7335;
ID = 0;
QuestionID = 28;
Response = "1In the two weeks before you felt sick, did you:";
SurveyID = 25;
}]
尝试使用以下函数调用网络服务
func postValues(requestParams: [[String: AnyObject]], urlString: String) {
let url = URL(string: urlString)
var request = URLRequest(url: url!)
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.httpMethod = "POST"
request.httpBody = try! JSONSerialization.data(withJSONObject: requestParams, options: [])
AF.request(request).responseJSON { (response) in
switch response.result {
case .success:
// print(response.result.value)
break
case .failure:
print(response.error)
break
}
}
}