尝试使用 Alamofire 放置方法时 JSON 写入 (_SwiftValue) 中的类型无效
Invalid type in JSON write (_SwiftValue) when trying to put method with Alamofire
我试图用 JSON 类型向服务器发出放置请求,但出现错误。
这是错误详细信息。
Terminating app due to uncaught exception
'NSInvalidArgumentException', reason: 'Invalid type in JSON write
(_SwiftValue)'
这是一个代码。
var attdList : JSON = [:]
...
var clientsAry = attdList["clients"]
...
let params = ["clients" : clientsAry, "submitted" : 1]
...
Alamofire.request(url + req_task, method: .put, parameters: params, encoding: JSONEncoding(options: []), headers:headers).responseJSON { response in
...
我打印了 params 变量,它显示如下。
有人可以帮忙吗?
▿ 1 element
▿ 0 : 2 elements
- key : "clients"
▿ value : [
{
"id" : null,
"hours" : "0",
"client" : {
"id" : 2,
"name" : "Anders Andersson"
}
},
{
"id" : null,
"hours" : "1",
"client" : {
"id" : 4,
"name" : "Gun Gunsson"
}
},
{
"id" : null,
"hours" : "2",
"client" : {
"id" : 3,
"name" : "Johan Johansson"
}
},
{
"id" : null,
"hours" : "3",
"client" : {
"id" : 1,
"name" : "Maria Martinsson"
}
}
]
▿ rawArray : 4 elements
▿ 0 : 3 elements
▿ 0 : 2 elements
- key : "id"
- value : <null>
▿ 1 : 2 elements
- key : "hours"
- value : "0"
▿ 2 : 2 elements
- key : "client"
▿ value : 2 elements
▿ 0 : 2 elements
- key : "id"
- value : 2
▿ 1 : 2 elements
- key : "name"
- value : Anders Andersson
▿ 1 : 3 elements
▿ 0 : 2 elements
- key : "id"
- value : <null>
▿ 1 : 2 elements
- key : "hours"
- value : "1"
▿ 2 : 2 elements
- key : "client"
▿ value : 2 elements
▿ 0 : 2 elements
- key : "id"
- value : 4
▿ 1 : 2 elements
- key : "name"
- value : Gun Gunsson
▿ 2 : 3 elements
▿ 0 : 2 elements
- key : "id"
- value : <null>
▿ 1 : 2 elements
- key : "hours"
- value : "2"
▿ 2 : 2 elements
- key : "client"
▿ value : 2 elements
▿ 0 : 2 elements
- key : "id"
- value : 3
▿ 1 : 2 elements
- key : "name"
- value : Johan Johansson
▿ 3 : 3 elements
▿ 0 : 2 elements
- key : "id"
- value : <null>
▿ 1 : 2 elements
- key : "hours"
- value : "3"
▿ 2 : 2 elements
- key : "client"
▿ value : 2 elements
▿ 0 : 2 elements
- key : "id"
- value : 1
▿ 1 : 2 elements
- key : "name"
- value : Maria Martinsson
- rawDictionary : 0 elements
- rawString : ""
- rawNumber : 0
- rawNull : <null>
- rawBool : false
- type : SwiftyJSON.Type.array
- error : nil
报错提示使用了非法类型(Parameters
),JSON仅支持string
、number
、<null>
和array
/ dictionary
.
您应该将 JSON 数据转换为字符串:
let jsonData = try JSONSerialization.dataWithJSONObject(attdList, options: .prettyPrinted) // first of all convert json to the data
let convertedString = String(data: jsonData, encoding: .utf8) // the data will be converted to the string
然后将您的数据发送到服务器:
let params = ["clients" : convertedString, "submitted" : 1]
...
Alamofire.request(url + req_task, method: .put, parameters: params, encoding: JSONEncoding(options: []), headers:headers).responseJSON { response in
...
我试图用 JSON 类型向服务器发出放置请求,但出现错误。 这是错误详细信息。
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid type in JSON write (_SwiftValue)'
这是一个代码。
var attdList : JSON = [:]
...
var clientsAry = attdList["clients"]
...
let params = ["clients" : clientsAry, "submitted" : 1]
...
Alamofire.request(url + req_task, method: .put, parameters: params, encoding: JSONEncoding(options: []), headers:headers).responseJSON { response in
...
我打印了 params 变量,它显示如下。 有人可以帮忙吗?
▿ 1 element
▿ 0 : 2 elements
- key : "clients"
▿ value : [
{
"id" : null,
"hours" : "0",
"client" : {
"id" : 2,
"name" : "Anders Andersson"
}
},
{
"id" : null,
"hours" : "1",
"client" : {
"id" : 4,
"name" : "Gun Gunsson"
}
},
{
"id" : null,
"hours" : "2",
"client" : {
"id" : 3,
"name" : "Johan Johansson"
}
},
{
"id" : null,
"hours" : "3",
"client" : {
"id" : 1,
"name" : "Maria Martinsson"
}
}
]
▿ rawArray : 4 elements
▿ 0 : 3 elements
▿ 0 : 2 elements
- key : "id"
- value : <null>
▿ 1 : 2 elements
- key : "hours"
- value : "0"
▿ 2 : 2 elements
- key : "client"
▿ value : 2 elements
▿ 0 : 2 elements
- key : "id"
- value : 2
▿ 1 : 2 elements
- key : "name"
- value : Anders Andersson
▿ 1 : 3 elements
▿ 0 : 2 elements
- key : "id"
- value : <null>
▿ 1 : 2 elements
- key : "hours"
- value : "1"
▿ 2 : 2 elements
- key : "client"
▿ value : 2 elements
▿ 0 : 2 elements
- key : "id"
- value : 4
▿ 1 : 2 elements
- key : "name"
- value : Gun Gunsson
▿ 2 : 3 elements
▿ 0 : 2 elements
- key : "id"
- value : <null>
▿ 1 : 2 elements
- key : "hours"
- value : "2"
▿ 2 : 2 elements
- key : "client"
▿ value : 2 elements
▿ 0 : 2 elements
- key : "id"
- value : 3
▿ 1 : 2 elements
- key : "name"
- value : Johan Johansson
▿ 3 : 3 elements
▿ 0 : 2 elements
- key : "id"
- value : <null>
▿ 1 : 2 elements
- key : "hours"
- value : "3"
▿ 2 : 2 elements
- key : "client"
▿ value : 2 elements
▿ 0 : 2 elements
- key : "id"
- value : 1
▿ 1 : 2 elements
- key : "name"
- value : Maria Martinsson
- rawDictionary : 0 elements
- rawString : ""
- rawNumber : 0
- rawNull : <null>
- rawBool : false
- type : SwiftyJSON.Type.array
- error : nil
报错提示使用了非法类型(Parameters
),JSON仅支持string
、number
、<null>
和array
/ dictionary
.
您应该将 JSON 数据转换为字符串:
let jsonData = try JSONSerialization.dataWithJSONObject(attdList, options: .prettyPrinted) // first of all convert json to the data
let convertedString = String(data: jsonData, encoding: .utf8) // the data will be converted to the string
然后将您的数据发送到服务器:
let params = ["clients" : convertedString, "submitted" : 1]
...
Alamofire.request(url + req_task, method: .put, parameters: params, encoding: JSONEncoding(options: []), headers:headers).responseJSON { response in
...