As mentioned in the title of post,I'm getting NSInvalidArgumentException - 'Invalid top-level type in JSON write' when trying to convert Dictionary to JSON Data in swift
let userInfo: [String: String] = [
"user_name" : username!,
"password" : password!,
"device_id" : DEVICE_ID!,
"os_version" : OS_VERSION
]
let inputData = jsonEncode(object: userInfo)
. . .
static private func jsonEncode(object:Any?) -> Data?
{
do{
if let encoded = try JSONSerialization.data(withJSONObject: object, options:[]) as Data? <- here occured NSInvalidArgumentException
if(encoded != nil)
{
return encoded
}
else
{
return nil
}
}
catch
{
return nil
}
}
I'm passing Dictionary as parameter, not getting whats going wrong. Please help me guys.
Thanks!