JSON 中的无效类型写入 (__SwiftValue)
Invalid type in JSON write (__SwiftValue)
我正在尝试将参数 [String:Any] 值传递给 JSONSerialization.data,但每次都抛出错误。
我知道 [String:Any] 字典的值是 Swifty.JSON 个对象。但我无法将它们转换为 NSDictionary 对象。
我像这样从另一个字典填充参数:
var params = [String:Any]()
for (key, value) in self.myDictionary[self.selectedEntry] as! JSON {
print("\(key) - \(value.description)")
params[key]=value
}
这是 print(params) 后 params 对象中的内容。
["searchOptions": {
"omit_saved_records" : false
}, "customFilters": {
"is_next_sellers_search" : "Y"
}, "use_code_std": [
"RSFR",
"RCON"
]]
我正在将参数传递给这个函数:
let json = try JSONSerialization.data(withJSONObject: params, options: .prettyPrinted)
这是错误发生的地方。
我希望这能简单地工作,但我却收到了这个错误:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid type in JSON write (__SwiftValue)'
我做错了什么?
SwiftyJSON
的JSON
类型是自定义类型,无法序列化。
只需从 JSON
对象中获取 dictionaryObject
if let params = self.myDictionary[self.selectedEntry].dictionaryObject {
并且不要 prettyPrint
。服务器不关心
let json = try JSONSerialization.data(withJSONObject: params)
注意:我们鼓励您放弃 SwiftyJSON
以支持 Codable
。
我正在尝试将参数 [String:Any] 值传递给 JSONSerialization.data,但每次都抛出错误。
我知道 [String:Any] 字典的值是 Swifty.JSON 个对象。但我无法将它们转换为 NSDictionary 对象。
我像这样从另一个字典填充参数:
var params = [String:Any]()
for (key, value) in self.myDictionary[self.selectedEntry] as! JSON {
print("\(key) - \(value.description)")
params[key]=value
}
这是 print(params) 后 params 对象中的内容。
["searchOptions": {
"omit_saved_records" : false
}, "customFilters": {
"is_next_sellers_search" : "Y"
}, "use_code_std": [
"RSFR",
"RCON"
]]
我正在将参数传递给这个函数:
let json = try JSONSerialization.data(withJSONObject: params, options: .prettyPrinted)
这是错误发生的地方。
我希望这能简单地工作,但我却收到了这个错误:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid type in JSON write (__SwiftValue)'
我做错了什么?
SwiftyJSON
的JSON
类型是自定义类型,无法序列化。
只需从 JSON
对象中获取 dictionaryObject
if let params = self.myDictionary[self.selectedEntry].dictionaryObject {
并且不要 prettyPrint
。服务器不关心
let json = try JSONSerialization.data(withJSONObject: params)
注意:我们鼓励您放弃 SwiftyJSON
以支持 Codable
。