I have set up an API that gives a JSON response as follows:
{
"key1": "success",
"key2": {
"int_val": 5,
"str_val": "email",
}
}
I've read this, yet still do not understand how I can access key1
properly. I have tried to decode the data in the transformer through [String : Any]
, which throws an ambiguous type error: "Type of expression is ambiguous".
So how can I read the response with Siesta in the code below?
service.resource("").request(.post, json: userJSON).onSuccess{ entity in
guard let data = entity.content as? Data else {
return
}
print(data)
}
You can try Decodable
struct Root:Decodable (
let key1:String
let key2:InnerItem
}
struct InnerItem:Decodable {
let intVal:Int
let strVal:String
}
do {
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
let res = decoder.decode(Root.self,from:data)
print(res.key1)
}
catch {
print(error)
}