问题
I'm using JSON decoder to decode some Instagram users. Here's the model:
struct User: Decodable {
var clear_client_cache: Int
var has_more: Int
var hashtags: String?
var places: String?
var rank_token: String?
var status: String?
var users: [Users]?
struct Users: Decodable {
var position: Int
var user: InstaUser?
struct InstaUser: Decodable {
var byline: String?
var follower_count: Int?
var full_name: String?
var has_anonymous_profile_picture: Int?
var is_verified: Int?
var mutual_followers_count: Int?
var pk: Int?
var profile_pic_id: String?
var profile_pic_url: String?
var unseen_count: Int?
var usernam: String?
}
}
}
{
"clear_client_cache" = 0;
"has_more" = 0;
hashtags = (
);
places = (
);
"rank_token" = "3b22a104-7ac5-4052-be10-e7a7be4e67b4";
status = ok;
users = (
);
}
typeMismatch(Swift.Int, Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "clear_client_cache", intValue: nil)], debugDescription: "Expected to decode Int but found a number instead.", underlyingError: nil))
Than I just use
let user = try? decoder.decode(User.self, from: response.data!)
It says that it wants a number, what's wrong? Sorry, maybe it's too simple, but I'm coding for 12 hours already
来源:https://stackoverflow.com/questions/49826140/unable-to-decode-a-json-with-json-decoder