嗨,
我想解码一些json数据,但收到错误“预期解码Dictionary,但找到一个数组。” ,我不知道如何解决它。
这是我的回复:
[
{
"firstUser":{
"firstname":"John",
"id":6
},
"id":0,
"secondUser":{
"firstname":"Peter",
"id":17
}
]
这就是我的解码功能的样子:
func result<T: Decodable>(model: T.Type) -> T? {
if !success { return nil }
guard let responseData = data else { return nil }
do {
return try JSONDecoder().decode(model, from: responseData)
} catch {
return nil <<<---- HERE i get that error
}
}
这是我的用法:
API.get(route: Constants.feedback) { response in
if !response.success {
return
}
let feedback = response.result(model: Feedback.self)
}
这些是我的模型:
final class Feedback: Codable {
let firstUser: User
let secondUser: User
let id: Int
}
final class User:Codable {
let firstname: String
let id:Int
}