多参数字典(集合列表)类似:[[String:Any]]到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;
}]

尝试以下功能以调用web服务

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
}
}
}

相关内容

最新更新