我尝试按以下格式在POST请求参数中发送数组:
{ "ratings": [{
"safety_rating_id": 105,
"schedule_job_id": 18,
"score": 9,
"submission_id": 27
},
{
"safety_rating_id": 105,
"schedule_job_id": 18,
"score": 9,
"submission_id": 27
}]
}
我试图包装我的数组但是我被抛出了这个错误:
上下文类型'数组' (又名'数组>')不能与字典文字
一起使用
POST请求代码:
let comments: Array<[String:AnyObject]> = [
"ratings" : [
[
"safety_rating_id" : 106,
"schedule_job_id" : 18,
"score" : 10,
"submission_id" : 27
],
[
"safety_rating_id" : 105,
"schedule_job_id" : 18,
"score" : 9,
"submission_id" : 27
]
]
]
Alamofire.request(.POST, endPoint, headers: Auth_header, parameters: comments)
.validate()
.responseData {
response in
switch response.result {
case .Success(let data):
let json = JSON(data)
print(json)
onCompletion()
case .Failure(let error):
print("Request failed with error: \(error)")
onError?(error)
}
}
答案 0 :(得分:0)
你写
let comments: Array<[String:AnyObject]>
但你放在那里的内容是一个字典,而不是一个数组。
将comments
的签名更改为简单字典:
let comments: [String:AnyObject] = [
"ratings" : [
[
"safety_rating_id" : 106,
"schedule_job_id" : 18,
"score" : 10,
"submission_id" : 27
],
[
"safety_rating_id" : 105,
"schedule_job_id" : 18,
"score" : 9,
"submission_id" : 27
]
]
]
或者,如果您需要完整类型:
let comments: [String:[[String:Int]]] = [
"ratings" : [
[
"safety_rating_id" : 106,
"schedule_job_id" : 18,
"score" : 10,
"submission_id" : 27
],
[
"safety_rating_id" : 105,
"schedule_job_id" : 18,
"score" : 9,
"submission_id" : 27
]
]
]