I am trying to get string value in a json but I keep getting nil.
This is json structure
{
"user": {
"password": "...",
"name": "...",
"authToken": "...",
"events": [
{
"uuid": (int)
}
]
}
}
this is how I am decoding.
do {
let json = try JSONSerialization.jsonObject(with: responseData, options:[]) as! [String : Any?]
let authToken: String = json["authToken"] as! String
print(authToken)
} catch let error {
print(error)
}
but I keep getting nil value in my console
CodableandJSONDecoder. Do not use the legacyJSONSerializationand force casting. In any case, you need to decode several levels, you cannot just accessauthToken, which is nested inside theuserDictionary of the JSON.